Click to See Complete Forum and Search --> : How do you read a file up to a certain point?


Windchill
06-08-2003, 03:01 PM
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).

pyro
06-08-2003, 04:18 PM
This will read through a .txt file and put all the content before [next] into the varialbe $good:

<?PHP

$filename = "test.txt";
$find_this_string = "\\[next\\]"; # backslashes added due to the fact that [ and ] are regexp special characters.
$new = "";

$contents = @file($filename) or die ("File $filename could not be opened.");

foreach ($contents as $line_num => $line) {
$new .= $line;
}

list ($good, $bad) = preg_split("/$find_this_string/", $new);

echo $good;

?>

and, to get the length of a string, you use strlen($string);

Windchill
06-08-2003, 06:30 PM
it there a way to do that but leave the file pointer at the point the last phrase left off?

pyro
06-08-2003, 06:50 PM
Huh? I don't think I understand what you are trying to do. Could you please explain what you need to do a bit better?

Windchill
06-09-2003, 08:06 AM
I am trying to read multiple paragraphs in to an array so I can edit them seperatly.(news stories)

pyro
06-09-2003, 08:15 AM
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

$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.");

foreach ($contents as $line_num => $line) {
$new .= $line;
}

list ($good, $bad) = preg_split("/$find_this_string/", $new);

$write = $good.$text.$bad;

$fp = fopen ($filename, "w");
if ($fp) {
fwrite ($fp, $text);
fclose ($fp);
echo ("File written");
}
else {
echo ("File was not written");
}

?>

Windchill
06-11-2003, 05:53 PM
Actually I wan to read all the seperate <p>'s into an array so I can edit multiple ones and re arange the sequence they are in.

pyro
06-11-2003, 09:50 PM
This will go through the file and pull all the <p>'s (note: must end with a </p>) from your file. It will then put them into the array $p.

<?PHP

$filename = "test.txt";
$new = "";

$contents = @file($filename) or die ("File $filename could not be opened.");

foreach ($contents as $line_num => $line) {
$new .= $line;
}

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
}

?>