Search String and Return Line PHP

后端 未结 5 740
傲寒
傲寒 2021-01-05 19:22

I\'m trying to search a PHP file for a string and when that string is found I want to return the whole LINE that the string is on. Here is my example code. I\'m thinking I

5条回答
  •  执念已碎
    2021-01-05 19:29

    Just read the whole file as array of lines using file function.

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

提交回复
热议问题