Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按输入值搜索文件_Php_File_Search - Fatal编程技术网

Php 按输入值搜索文件

Php 按输入值搜索文件,php,file,search,Php,File,Search,我在文件搜索方面有问题。我有两个文本表单,我想在其中输入搜索文件的值。但不知何故,如果我在其中一个表单中写入输入,它也会显示在另一行中(我不想要这个)。此外,我的搜索仅通过其中一个搜索选项(其中变量为$search)起作用 也许你能给我点什么或者告诉我我做错了什么 您没有关闭已创建的函数:函数f_display(){ 您使用的是不推荐使用的HTML,您不使用引号来给出值,而是直接给出值。好方法是: 您在编写时使用了不安全的方法,如$\u SERVER['PHP\u SELF'] 您正在调用未定义

我在文件搜索方面有问题。我有两个文本表单,我想在其中输入搜索文件的值。但不知何故,如果我在其中一个表单中写入输入,它也会显示在另一行中(我不想要这个)。此外,我的搜索仅通过其中一个搜索选项(其中变量为$search)起作用

也许你能给我点什么或者告诉我我做错了什么

  • 您没有关闭已创建的函数:
    函数f_display(){
  • 您使用的是不推荐使用的HTML,
    您不使用引号来给出值,而是直接给出值。好方法是:
  • 您在编写时使用了不安全的方法,如
    $\u SERVER['PHP\u SELF']
  • 您正在调用未定义的变量
    $\u GET['search']
    (因为在第一次加载时它们还没有定义),这可能会产生错误。(提示:使用filter\u input()
  • 您正在使用HTML5中不再使用的
    width=50%
  • 您使用的是explode方法,我不确定为什么不使用strpos(位于单词所在的位置,这就是您正在尝试的)
  • 如果要逐行导出到最后一个,fgets需要它的第二个参数:4096(从中提取,我以前从未使用过该方法)
  • 您在哪里定义$tr_spec var
  • 现在,我将为您提供我的PHP脚本更正,如果您想使用它,请随意使用:

      // Check if the form has been sent or if the page was accessed using the GET method.
      if( filter_input( INPUT_SERVER, "REQUEST_METHOD" ) !== "GET" ) {
        die();
      }
    
      $search = filter_input( INPUT_GET, "search" );
    
      //  Check if the searh input has been correctly fill
      if( !isset( $search ) ) {
        die();
      }
    
      // Let's return here to prevent open an empty file
      if( filesize( $file ) <= 0 ) {
        return;
      }
    
      $openedFile = fopen( $file, "r" );
    
      // I was using the here document option, echo <<<END END; but it didn't work due to my text editor...
      echo "
      <table style=\"width: 50%; border-width: 1px;\">
        <thead>
          <tr>
            <th>&nbsp;</th>
            <th>vardas</th>
            <th>nikas</th>
            <th>pranesimas</th>
          </tr>
        </thead>
        <tbody>
      ";
    
      // Let's save in buffer the current line and then navigate through all of them
    
      while( ( $buffer = fgets( $openedFile, 4096 ) ) !== false ) {
    
        // Here we check if the line has the searched word
        if( strpos( $buffer, $search ) !== false ) {
    
          $part = explode( "|", $buffer );
    
          echo "
              <tr>
                <td>" . $part[0] . "</td>
                <td>" . $part[1] . "</td>
                <td>" . $part[2] . "</td>
                <td>" . $part[3] . "</td>
              </tr>
          ";
    
        }//.if
    
      }//.while
    
      echo "
        </tbody>
      </table>
      ";
    
    //检查表单是否已发送,或者是否使用GET方法访问了页面。
    如果(过滤输入(输入服务器,“请求方法”)!=“获取”){
    模具();
    }
    $search=过滤输入(输入获取“搜索”);
    //检查searh输入是否已正确填充
    如果(!isset($search)){
    模具();
    }
    //让我们回到这里,以防止打开空文件
    
    if(文件大小($file)如果我想分别搜索两个部分,该怎么做呢?你的脚本在整个文件中搜索,我需要按两个部分搜索,这是什么意思?如果你是指两个不同的文件,只需在第一个文件之前添加一段时间,如果你只想更改分解或使用strpos排除部分,你也可以使用substr方法我的意思是文件有3列,每列都是一部分。我需要在第1列和第3列中进行搜索。我可以轻松搜索一列,但无法在两列(第[0]部分和第[2]部分)中进行搜索。否!strpos()方法将查找列中的每个单词。如果您只想测试列是否正常,请首先分解代码,然后对每个部分执行strpos。也许您可以向我介绍如何使用表单?如何进行两种不同的输入,一种用于部分[1],另一种用于部分[2]。然后进行搜索,这取决于插入的值。
    John|J0N|Hello
    Peter|P3tE|Wow
    Paul|PO|Nice
    
      // Check if the form has been sent or if the page was accessed using the GET method.
      if( filter_input( INPUT_SERVER, "REQUEST_METHOD" ) !== "GET" ) {
        die();
      }
    
      $search = filter_input( INPUT_GET, "search" );
    
      //  Check if the searh input has been correctly fill
      if( !isset( $search ) ) {
        die();
      }
    
      // Let's return here to prevent open an empty file
      if( filesize( $file ) <= 0 ) {
        return;
      }
    
      $openedFile = fopen( $file, "r" );
    
      // I was using the here document option, echo <<<END END; but it didn't work due to my text editor...
      echo "
      <table style=\"width: 50%; border-width: 1px;\">
        <thead>
          <tr>
            <th>&nbsp;</th>
            <th>vardas</th>
            <th>nikas</th>
            <th>pranesimas</th>
          </tr>
        </thead>
        <tbody>
      ";
    
      // Let's save in buffer the current line and then navigate through all of them
    
      while( ( $buffer = fgets( $openedFile, 4096 ) ) !== false ) {
    
        // Here we check if the line has the searched word
        if( strpos( $buffer, $search ) !== false ) {
    
          $part = explode( "|", $buffer );
    
          echo "
              <tr>
                <td>" . $part[0] . "</td>
                <td>" . $part[1] . "</td>
                <td>" . $part[2] . "</td>
                <td>" . $part[3] . "</td>
              </tr>
          ";
    
        }//.if
    
      }//.while
    
      echo "
        </tbody>
      </table>
      ";