dongzhong1891 2017-12-23 10:20
浏览 154

如何使用PHP在文本文件中搜索字符串

i have a php code that read TXT file and display its content.

i want to allow the user to make a search on any word he want and if its available the system will display it with the line number.

until now i was able to read the text and display it .

i know that i need to read it line by line and stored in variable right or it there any better options?

code

<?php


    $myFile = "test.txt";

    $myFileLink = fopen($myFile, 'r');

    $myFileContents = fread($myFileLink, filesize($myFile));

    while(!feof($myFileContents)) { 
            echo fgets($myFileContents) . "<br />";
        }

    if(isset($_POST["search"]))
    {
        $search =$_POST['name'];
        $myFileContents = str_replace(["
",""], "
",  $myFileContents);
        if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches,PREG_OFFSET_CAPTURE))
        {
            foreach($matches[1] as $match)
            {
            $line = 1+substr_count(substr($myFileContents,0,$match[1]), "
");
            echo "Found $search on $line";
            }
        }
    }


    fclose($myFileLink);

    //echo $myFileContents; 
    ?>

    <html>
        <head>

        </head>
        <body>
         <form action="index.php" method="post">
              <p>enter your string <input type ="text"  id = "idName"  name="name" /></p>
              <p><input type ="Submit" name ="search" value= "Search" /></p>
        </form>
        </body>
    </html>
  • 写回答

1条回答 默认 最新

  • douzhe9075 2017-12-23 10:27
    关注

    Something like this

    $myFileContents = file_get_contents($myFileLink);
    
    if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches)){
        print_r($matches);
    }
    

    Using Preg match all, and case insensitive flag.

    Getting the line number is much harder, for that you will want to do something like this:

    $myFileContents = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
    minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
    Excepteur sint occaecat cupidatat non proident,
    sunt in culpa qui officia deserunt mollit anim id est laborum.";
    
    $search = "non proident";
    
    //normalize line endings.
    $myFileContents = str_replace(["
    ",""], "
    ",  $myFileContents);
    
        //Enter your code here, enjoy!
    if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches,PREG_OFFSET_CAPTURE)){
    
        foreach($matches[1] as $match){
            $line = 1+substr_count(substr($myFileContents,0,$match[1]), "
    ");
            echo "Found $search on $line";
        }
    
    }
    

    Outputs

    Found non proident on 5
    

    You can see it live here

    If it's a large file you can do a similar search as you read each line. Something like this

     $myFileLink = fopen($myFile, 'r');
    
     $line = 1; 
    
     while(!feof($myFileLink)) { 
         $myFileContents = fgets($myFileLink);
         if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches)){
            foreach($matches[1] as $match){
               echo "Found $match on $line";
            }
         }
         ++$line;
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?