第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

      <em id="pjw93"><option id="pjw93"></option></em>
    1. 為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
      已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

      PHP在txt文件中搜索并回顯整行

      PHP在txt文件中搜索并回顯整行

      PHP
      哆啦的時(shí)光機(jī) 2019-10-10 14:28:24
      我正在嘗試使用php創(chuàng)建一個(gè)腳本,該腳本將在文本文件中搜索并獲取整行并回顯它。我有一個(gè)名為“ numorder.txt”的文本文件(.txt),并且在該文本文件中有幾行數(shù)據(jù),每5分鐘會(huì)有新行出現(xiàn)(使用cron作業(yè))。數(shù)據(jù)類似于:2 aullah17 name12 username我將如何創(chuàng)建一個(gè)php腳本來搜索數(shù)據(jù)“ aullah1”,然后抓起整行并回顯它?(一旦回顯,它應(yīng)該顯示“ 2 aullah1”(不帶引號(hào))。如果我沒有清楚地解釋任何事情和/或您想讓我更詳細(xì)地解釋,請(qǐng)發(fā)表評(píng)論。
      查看完整描述

      3 回答

      ?
      心有法竹

      TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

      還有一個(gè)PHP示例,將顯示多行匹配:


      <?php

      $file = 'somefile.txt';

      $searchfor = 'name';


      // the following line prevents the browser from parsing this as HTML.

      header('Content-Type: text/plain');


      // get the file contents, assuming the file to be readable (and exist)

      $contents = file_get_contents($file);

      // escape special characters in the query

      $pattern = preg_quote($searchfor, '/');

      // finalise the regular expression, matching the whole line

      $pattern = "/^.*$pattern.*\$/m";

      // search, and store all matching occurences in $matches

      if(preg_match_all($pattern, $contents, $matches)){

         echo "Found matches:\n";

         echo implode("\n", $matches[0]);

      }

      else{

         echo "No matches found";

      }


      查看完整回答
      反對(duì) 回復(fù) 2019-10-10
      ?
      小唯快跑啊

      TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊

      像這樣做。這種方法可以讓你搜索一個(gè)任意大小的文件(大尺寸不會(huì)崩潰的腳本),并返回匹配的所有行你想要的字符串。


      <?php

      $searchthis = "mystring";

      $matches = array();


      $handle = @fopen("path/to/inputfile.txt", "r");

      if ($handle)

      {

          while (!feof($handle))

          {

              $buffer = fgets($handle);

              if(strpos($buffer, $searchthis) !== FALSE)

                  $matches[] = $buffer;

          }

          fclose($handle);

      }


      //show results:

      print_r($matches);

      ?>

      注意,該方法strpos與!==運(yùn)算符一起使用。


      查看完整回答
      反對(duì) 回復(fù) 2019-10-10
      ?
      尚方寶劍之說

      TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊

      使用file()和strpos():


      <?php

      // What to look for

      $search = 'foo';

      // Read from file

      $lines = file('file.txt');

      foreach($lines as $line)

      {

        // Check if the line contains the string we're looking for, and print if it does

        if(strpos($line, $search) !== false)

          echo $line;

      }

      在此文件上進(jìn)行測(cè)試時(shí):


      foozah 

      barzah 

      abczah


      它輸出:


      富扎


      更新:

      如果未找到文本,則顯示文本,請(qǐng)使用類似以下內(nèi)容的方法:


      <?php

      $search = 'foo';

      $lines = file('file.txt');

      // Store true when the text is found

      $found = false;

      foreach($lines as $line)

      {

        if(strpos($line, $search) !== false)

        {

          $found = true;

          echo $line;

        }

      }

      // If the text was not found, show a message

      if(!$found)

      {

        echo 'No match found';

      }

      在這里,我使用$found變量來查找是否找到匹配項(xiàng)。


      查看完整回答
      反對(duì) 回復(fù) 2019-10-10
      • 3 回答
      • 0 關(guān)注
      • 864 瀏覽

      添加回答

      舉報(bào)

      0/150
      提交
      取消
      微信客服

      購(gòu)課補(bǔ)貼
      聯(lián)系客服咨詢優(yōu)惠詳情

      幫助反饋 APP下載

      慕課網(wǎng)APP
      您的移動(dòng)學(xué)習(xí)伙伴

      公眾號(hào)

      掃描二維碼
      關(guān)注慕課網(wǎng)微信公眾號(hào)