Reputation: 45410
I have a file I am reading in with php
:
$lines = file("input.txt");
I need to search $lines
for the string --><--
then return the very next full line. What is the best way to do this?
Thanks.
Upvotes: 0
Views: 122
Reputation: 7880
I wasn't sure if you were matching a Windows or a Unix file (hence the \r?
), but you can do it with Regular Expressions much more cleanly. You can capture it like this:
$lines = file("input.txt");
preg_match('!(--><--\r?\n)([^\r\n]+)\r?\n!s',$lines,$matches);
$myLine = $matches[2];
echo $myLine;
Upvotes: 0
Reputation: 4634
You should loop through the array $lines
and increment the key by 1 to get the next line.
$lines = file("input.txt");
$return = array();
foreach($lines as $key => $line) {
if(false !== strpos($line,'--><--')) {
// check if there is another line in the file
if(isset($lines[($key+1)])) {
// add the line to array
$return[] = $lines[($key+1)];
}
}
}
print_r($return);
Upvotes: 1