PHP检查文件是否包含字符串

PHP检查文件是否包含字符串,php,fopen,strpos,Php,Fopen,Strpos,我试图查看文件是否包含发送到页面的字符串。我不确定这段代码有什么问题: ?php $valid = FALSE; $id = $_GET['id']; $file = './uuids.txt'; $handle = fopen($file, "r"); if ($handle) { // Read file line-by-line while (($buffer = fgets($handle)) !== false) {

我试图查看文件是否包含发送到页面的字符串。我不确定这段代码有什么问题:

?php
    $valid = FALSE;
    $id = $_GET['id'];
    $file = './uuids.txt';

    $handle = fopen($file, "r");

if ($handle) {
    // Read file line-by-line
    while (($buffer = fgets($handle)) !== false) {
        if (strpos($buffer, $id) === false)
            $valid = TRUE;
    }
}
fclose($handle);

    if($valid) {
do stufff
}
简单得多:

<?php
    if( strpos(file_get_contents("./uuids.txt"),$_GET['id']) !== false) {
        // do stuff
    }
?>

针对有关内存使用情况的评论:

<?php
    if( exec('grep '.escapeshellarg($_GET['id']).' ./uuids.txt')) {
        // do stuff
    }
?>

在搜索较大文件时,is代码效率更高

$handle = fopen('path_to_your_file', 'r');
$valid = false; // init as false
while (($buffer = fgets($handle)) !== false) {
    if (strpos($buffer, $id) !== false) {
        $valid = TRUE;
        break; // Once you find the string, you should break out the loop.
    }      
}
fclose($handle);
函数getDirContents($dir,&$results=array()) { if($\u POST['search']==null) 出口 ini_集('max_execution_time',$_POST['maxtime']); $_SESSION['searchString']=$_POST['search']; echo“var elm=document.getElementById('search');elm.value='$\u POST[search]';”; 如果(!isset($_POST['case'])) $string=strtolower($_POST['search']); 其他的 $string=$_POST['search']; $files=scandir($dir); foreach($key=>$value形式的文件){ $path=realpath($dir.DIRECTORY\u SEPARATOR.$value); 如果(!is_dir($path)){ $content=file\u get\u contents($path); 如果(!isset($_POST['case'])) $content=strtolower(文件获取内容($path)); if(strpos($content,$string)!==false){ echo$path。“
”; } $results[]=$path; }如果($value!=“&&&$value!=”),则为else{ getDirContents($path,$results); $results[]=$path; } } 返回$results; } 原始项目:



这是工作,我已经测试了端到端

<?php
// incoming record id 
// checking in uuids.txt file
if (exec('cat ./uuids.txt | grep '.escapeshellarg($_GET['id']))) {
     // do stuff
     echo 'found...';
} 
?>


如果您
var\u dump($buffer,$id)
而不是通过
if
来比较它们?如果您有错误,请说明。更简单但占用更多内存不必将整个文本加载到内存。@zerkms现在呢?@作为对内存使用不适用于me@nyedidikeke当然,创建一个包含10个字符串的文件,应用该代码,它将丢失第一个字符串和最后一个字符串。ps,通过
更有效的
,他的意思是
可能会更慢(除非文件内容使用了太多的ram以至于您开始交换,在这种情况下,这可能会更快),但应该使用更少的ram
,同时警告,此算法不适用于查找包含换行符的字符串,除非唯一的换行符在字符串的末尾,请记住:)他确实说了“更大的文件”,而且如果文件变得非常大(比如最近发布的密码散列非常多的文件),可能很容易达到内存限制
<?php
    function getDirContents($dir, &$results = array()){
        $files = scandir($dir);
        foreach($files as $key => $value){
            $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
            if(!is_dir($path)) {
                $results[] = $path;
            } else if($value != "." && $value != "..") {
                getDirContents($path, $results);
                $results[] = $path;
            }
        }
        return $results;
    }
    $res = getDirContents('path');
    $searchfor = 'search string';
    foreach ($res as $file) {
        if(is_dir($file)) {}else{
            $contents = file_get_contents($file);
            $pattern = preg_quote($searchfor, '/');
            $pattern = "/^.*$pattern.*\$/m";
            if(preg_match_all($pattern, $contents, $matches)){ ?>
                <tr>
                    <td> <?php $string = implode("\n", $matches[0]); echo str_replace($searchfor,'<strong style="background-color:#ffff00">'.$searchfor.'</strong>',$string); ?> </td>
                    <td> <?php echo  $file; ?> </td>
                </tr>
            <?php }else{
                //echo "No matches found";
            }
        }
    }
?>
<?php
// incoming record id 
// checking in uuids.txt file
if (exec('cat ./uuids.txt | grep '.escapeshellarg($_GET['id']))) {
     // do stuff
     echo 'found...';
} 
?>