我正在尝试在PHP文件中搜索字符串,当找到该字符串时,我想返回字符串所在的整个LINE.这是我的示例代码.我想我将不得不使用爆炸,但无法解决这个问题.
$searchterm = $_GET['q'];
$homepage = file_get_contents('forms.PHP');
if(strpos($homepage, "$searchterm") !== false)
{
echo "FOUND";
//OUTPUT THE LINE
}else{
echo "NOTFOUND";
}
解决方法:
function getLineWithString($fileName, $str) {
$lines = file($fileName);
foreach ($lines as $lineNumber => $line) {
if (strpos($line, $str) !== false) {
return $line;
}
}
return -1;
}