/    Sign up×
Community /Pin to ProfileBookmark

How do you read a file up to a certain point?

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).

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@pyroJun 08.2003 — This will read through a .txt file and put all the content before [next] into the varialbe $good:

[code=php]<?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;

?>[/code]


and, to get the length of a string, you use strlen($string);
Copy linkTweet thisAlerts:
@WindchillauthorJun 08.2003 — it there a way to do that but leave the file pointer at the point the last phrase left off?
Copy linkTweet thisAlerts:
@pyroJun 08.2003 — 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?
Copy linkTweet thisAlerts:
@WindchillauthorJun 09.2003 — I am trying to read multiple paragraphs in to an array so I can edit them seperatly.(news stories)
Copy linkTweet thisAlerts:
@pyroJun 09.2003 — 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)

[code=php]<?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");
}

?>[/code]
Copy linkTweet thisAlerts:
@WindchillauthorJun 11.2003 — 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.
Copy linkTweet thisAlerts:
@pyroJun 12.2003 — 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.

[code=php]<?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
}

?>[/code]
×

Success!

Help @Windchill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...