搜索字符串和返回行PHP

Nin*_*ist 5 php line file-get-contents strpos

我正在尝试在PHP文件中搜索字符串,当找到该字符串时,我想返回字符串所在的整个LINE.这是我的示例代码.我想我将不得不使用爆炸,但无法解决这个问题.

$searchterm =  $_GET['q'];

$homepage = file_get_contents('forms.php');

 if(strpos($homepage, "$searchterm") !== false)
 {
 echo "FOUND";

 //OUTPUT THE LINE

 }else{

 echo "NOTFOUND";
 }
Run Code Online (Sandbox Code Playgroud)

mar*_*tin 14

只需使用file函数将整个文件读作行数组即可.

function getLineWithString($fileName, $str) {
    $lines = file($fileName);
    foreach ($lines as $lineNumber => $line) {
        if (strpos($line, $str) !== false) {
            return $line;
        }
    }
    return -1;
}
Run Code Online (Sandbox Code Playgroud)