Let's say I hade a variable ($find_this_string="[next]") and I wanted to read everything in a file up to $find_this_string into a variable. How would I do this? I know you can read x number of charectors but what if it's a variable number of charectors? Also how do I get the length of a string(in charecters).
So, you are opening a HTML file, and trying to pull certain <p>'s out so you can edit them? If this is the case, I would recommend adding comments to your page, like this <!--editable--> if possible. Then, the PHP file can split it at each <!--editable--> and you will have an array of each of the <p>'s you want to edit... Either way, what you are going to have to do when you write it back is something like this: (using my previous code, assumbing you want to replace the [next] with your text)
PHP Code:
<?PHP
$filename = "test.txt";
$find_this_string = "\\[next\\]"; # backslashes added due to the fact that [ and ] are regexp special characters.
$text = "This is the text to add";
$new = "";
$contents = @file($filename) or die ("File $filename could not be opened.");
preg_match_all("/\<p\>(.+?)\<\/p\>/", $new, $p);
#If you don't want to pull empty <p>'s (<p></p>) use the below regexp
#preg_match_all("/\<p\>(.+?)\<\/p\>/", $new, $p);
$p = $p[0]; #set $p to be an array of all the <p>'s
foreach ($p as $line_num => $line) {
echo $line; #echo <p>'s to the screen
}
Bookmarks