乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      PHP:如何從文本文件中刪除最后一個換行符?

       印度阿三17 2019-10-28

      在學校工作中,我使用PHP在我的城市中為虛構的太空博物館建立了一個站點.它具有數(shù)據(jù)包含和數(shù)據(jù)咨詢系統(tǒng),但是我有一個咨詢問題,我想知道該如何解決:如何從文件中刪除最后一個換行符?

      數(shù)據(jù)包含

      在站點的受限區(qū)域中,我有一個HTML5表單,其中包含5個字段(名稱,地址,電話號碼,性別和參觀的展覽),該表單通過POST方法將數(shù)據(jù)發(fā)送到PHP中的函數(shù),該函數(shù)將其寫入給定的txt文件中通過使用fwrite命令:

      fwrite ($pointer, "$name | $addres | $telephone | $sex | $exhibitions " .PHP_EOL);
      

      如您所見,它在txt文件中寫入在表單上輸入的數(shù)據(jù),以及一個換行符.指針是fopen用于打開我需要工作的文件的變量.輸出示例:

      Márcio Aguiar | Belmiro Braga Street | 1234-5678 | M | Planets of Solar System
      Joana Tobias | Santos Dummont Avenue | 8765-4321 | F | Black Holes, Satellites

      資料咨詢

      然后是一個咨詢系統(tǒng).它具有一個循環(huán),直到文件結束為止.在此循環(huán)中,有一個名為$buffer的變量,該變量每次都獲取txt文件的一行.然后將其分解以創(chuàng)建一個名為$lines [$counter]的數(shù)組.為了更好地打印它,我使用了array_combine,其中將另一個數(shù)組($keys)上的字段名稱連接到以$lines [$counter]編寫的值,并將其附加到$combined [$counter].然后循環(huán)結束,我在< pre>< / pre>中使用了print_r.查看以$combined編寫的數(shù)據(jù),同時保留HTML否則會忽略的空格和中斷.這是代碼:

      $keys = array ("Name", "Address", "Telephone", "Sex", "Visited exhibition");
      for ($counter=0;!feof($reader);$counter  ){
          $buffer = fgets($reader);
          $lines[$counter] = explode(" | ", $buffer);
          $combined[$counter] = array_combine($keys, $lines[$counter]);
      }
      echo "<pre>";
      print_r($combined);
      echo "</pre>";
      

      輸出示例:

      06002

      在這里您可以看到2數(shù)組已創(chuàng)建為空白.這是由最后一行引起的,該行僅包含上面表格插入的換行符.我需要刪除最后一個換行符,并且只刪除那個,但不知道如何.我想知道!當執(zhí)行到達array_combine時,不知道會導致錯誤顯示,因為需要兩個數(shù)組具有相同數(shù)量的元素,并且2為空.這里的錯誤:

      Warning: array_combine(): Both parameters should have an equal number of elements in E:\Aluno\Documents\Wamp\www\trab_1\area_restrita\consulta.php on line 60

      解決方法:

      原始答案:

      要從任何文本中刪除尾隨換行符,可以使用trim(),但是對于您而言,您只需要在append mode中使用fopen:

      $handle = fopen("/path/to/file", "a");
      

      然后擺脫PHP_EOL:

      fwrite ($pointer, "$name | $addres | $telephone | $sex | $exhibitions");
      

      編輯:您是對的,追加不會追加到新行.我誤解了.因此,您可以像我之前提到的那樣使用trim().我使用file_get_contents()和file_put_contents()創(chuàng)建了一個簡單的示例,它似乎可以滿足您的要求:

      <?php
      
      $file = 'test.txt';
      
      // Set existing contents (for testing sake)
      $orig_contents = "bob | 123 fake street | 1234567890 | yes, please | no\n";
      $orig_contents .= "bob | 123 fake street | 1234567890 | yes, please | no\n";
      $orig_contents .= "bob | 123 fake street | 1234567890 | yes, please | no\n";
      $orig_contents .= "bob | 123 fake street | 1234567890 | yes, please | no\n";
      file_put_contents($file, $orig_contents);
      
      // Here is how you could add a single new line with append mode
      // Notice the trailing \n
      file_put_contents($file, "billy | 456 fake street | 2345678901 | no | yes\n", FILE_APPEND);
      
      // Get contents from the file and remove any trailing line breaks
      $contents = trim(file_get_contents($file));
      
      $keys = array ("Name", "Address", "Telephone", "Sex", "Visited exhibition");
      
      // Explode based on the new line character
      $lines = explode("\n", $contents);
      
      foreach ($lines as $line) {
          $values = explode(" | ", $line);
          $combined[] = array_combine($keys, $values);
      }
      
      print_r($combined);
      

      打?。?/p>

      Array
      (
          [0] => Array
              (
                  [Name] => bob
                  [Address] => 123 fake street
                  [Telephone] => 1234567890
                  [Sex] => yes, please
                  [Visited exhibition] => no
              )
      
          [1] => Array
              (
                  [Name] => bob
                  [Address] => 123 fake street
                  [Telephone] => 1234567890
                  [Sex] => yes, please
                  [Visited exhibition] => no
              )
      
          [2] => Array
              (
                  [Name] => bob
                  [Address] => 123 fake street
                  [Telephone] => 1234567890
                  [Sex] => yes, please
                  [Visited exhibition] => no
              )
      
          [3] => Array
              (
                  [Name] => bob
                  [Address] => 123 fake street
                  [Telephone] => 1234567890
                  [Sex] => yes, please
                  [Visited exhibition] => no
              )
      
          [4] => Array
              (
                  [Name] => billy
                  [Address] => 456 fake street
                  [Telephone] => 2345678901
                  [Sex] => no
                  [Visited exhibition] => yes
              )
      
      )
      
      來源:https://www./content-1-529801.html

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多