I am trying to search multiple matched lines from a html file and return those lines.
If there is single match then it works. But if there are multiple matches it returns nothing.
Here is the code:
$line = getLineFromFile("abc.html", 'http://www.abc.com/');
echo $line;
function getLineFromFile($file, $string) {
$lines = file($file);
foreach($lines as $lineNumber => $line) {
if(strpos($line, $string) !== false){
return $lines[$lineNumber];
}
}
return false;
}
Why isn’t it returning all the matched lines?
Once you return from a function that function call stops executing. You’ll need to store your results in an array and then return it.
Just make sure you check for an empty array and not
falsewhen checking the results of this function.