我正在尝试在 PHP 文件中搜索一个字符串,当找到该字符串时,我想返回该字符串所在的整个 LINE。这是我的示例代码。我在想我必须使用爆炸但无法弄清楚。
$searchterm = $_GET['q'];
$homepage = file_get_contents('forms.php');
if(strpos($homepage, "$searchterm") !== false)
{
echo "FOUND";
//OUTPUT THE LINE
}else{
echo "NOTFOUND";
}
最佳答案
只需使用 file
将整个文件读取为行数组功能。
function getLineWithString($fileName, $str) {
$lines = file($fileName);
foreach ($lines as $lineNumber => $line) {
if (strpos($line, $str) !== false) {
return $line;
}
}
return -1;
}
关于php - 搜索字符串和返回行 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9721952/