PHP在模型中从控制器中的文件中查找字符串

PHP在模型中从控制器中的文件中查找字符串,php,string,model,controller,find,Php,String,Model,Controller,Find,我从控制器加载一个文件,并试图在模型中从该文件中找到一个字符串 我编写的控制器中的函数有: <?php public function file() { $filename = "C:/Users/Declan/Desktop/foo.txt"; } ?> 在模型中,我会: <?php function find{ if( exec('grep '.escapeshellarg($_GET['bar']).'$fil

我从控制器加载一个文件,并试图在模型中从该文件中找到一个字符串

我编写的控制器中的函数有:

<?php
    public function file() {
        $filename = "C:/Users/Declan/Desktop/foo.txt";
    }
?>

在模型中,我会:

<?php
    function find{
        if( exec('grep '.escapeshellarg($_GET['bar']).'$filename')) {
            echo "string found";
        }
    }
?>

我用它作为参考

如果您使用用于运行外部程序的exec()方法,将不胜感激。

。 显然,您是在windows上,grep仅适用于unix系统(Linux…)


执行
find()
函数与
file()
函数在同一类中如果find结尾缺少括号,则应为
find()
<?php
    function find($filePath, $stringToLookFor){
       $handle = fopen($filePath, 'r');

        $stringFound = false; // Init false
        while (($buffer = fgets($handle)) !== false) { // Loop through each file line
            if (strpos($buffer, $stringToLookFor) !== false) { // Check if in $buffer
                $stringFound = true; // String found
            }      
        }
        fclose($handle);
        return $stringFound;
    }
?>