doufu1950 2018-08-14 15:58
浏览 115
已采纳

substr not working | 仅显示数组的第一个值的输出

I want to use just the filename from each file for my further coding. How can i do that. I used substr but it is working just on the first line. Array $S2 holds:

access.2018.08.09.log|201808101105
access.2018.08.12.log|201808101105
access.2018.08.13.log|201808101105

I want just the text before '|' :-

access.2018.08.09.log
access.2018.08.12.log
access.2018.08.13.log

Code:-

<pre><?php

$files = scandir('C:\wamp64\www\MyLogs\logsfiles');
foreach($files as $key => $file) {
    if($file == '.' || $file == '..') unset($files[$key]);
}

$S2 = explode("
", substr(file_get_contents('uplodedregistry.txt'),0,21));

$result = array_diff($files, $S2);
print_r($result);

?>
  • 写回答

1条回答 默认 最新

  • dtpoius74857 2018-08-14 16:11
    关注

    The problem is the order you are doing this

    $S2 = explode("
    ", substr(file_get_contents('uplodedregistry.txt'),0,21));
    

    You need to explode the lines, loop through them then do the sub-string if you want all the entries.

    AS it stands you just grab the first one, then attempt to explode it on line endings which it doesn't have.

    If we step through what your code does.

    1 - get contents (added ', and for readability)

    'access.2018.08.09.log|201808101105
    
    access.2018.08.12.log|201808101105
    
    access.2018.08.13.log|201808101105
    '
    

    2 - sub string 0 to 21 of the above string

    'access.2018.08.09.log'
    

    3 - explode with the above string

    ['access.2018.08.09.log']
    

    Instead do something like this:

      $content = explode("
    ", file_get_contents('uplodedregistry.txt'));
    
      foreach($content AS &$row){
           $row = substr($row,0,21);
      }
    

    Note - updated by reference using the &. This way we don't have to make a new array.

    In contrast to the above this is what this does:

    1 - (Same as above)

    'access.2018.08.09.log|201808101105
    
    access.2018.08.12.log|201808101105
    
    access.2018.08.13.log|201808101105
    '
    

    2 - explode above string on

    array(
      'access.2018.08.09.log|201808101105',
      'access.2018.08.12.log|201808101105',
      'access.2018.08.13.log|201808101105'
    )
    

    3 - Foreach element (loop through above array)

    //iteration 1.  substr('access.2018.08.09.log|201808101105',0,21);
    //iteration 2.  substr('access.2018.08.12.log|201808101105',0,21);
    //iteration 3.  substr('access.2018.08.13.log|201808101105',0,21);
    

    Then because its update by reference if you do print_r($content) You should have this array

     array(
        'access.2018.08.09.log',
        'access.2018.08.12.log',
        'access.2018.08.13.log'
    )
    

    Also you can remove this loop

    $files = scandir('C:\wamp64\www\MyLogs\logsfiles');
    foreach($files as $key => $file) {
       if($file == '.' || $file == '..') unset($files[$key]);
    }
    

    By using

    $files = array_diff(scandir('C:\wamp64\www\MyLogs\logsfiles'), ['.','..']);
    

    Array diff returns the entries from Array1 that are not present in any of the argument arrays. So in this case it will return everything but . and ... The nice thing about this method is it's easy to add more files to the list, if there is other files you wish to exclude. It's also a lot cleaner and takes only 1 line.

    Lastly I should mention there are other methods you could use to do this, for example

       preg_match_all('/^([^|]+)/', file_get_contents('uplodedregistry.txt'), $matches);
    

    Or probably the best way is to use CSV reading, but use the pipe | instead of , as the delimiter.

    $f = fopen('uplodedregistry.txt', 'r');
    $contents = [];
    while (FALSE !== ($row = fgetcsv($f, 1000, "|"))){
          $contents[] = $row[0];
    }
    

    I would really consider using CSV

    fgetcsv ( resource $handle, int $length = 0, string $delimiter = ",", string $enclosure = '"' , string $escape = "\")

    http://php.net/manual/en/function.fgetcsv.php

    I mention these because sometimes you can have issues exploding on line endings depending on what OS creates the file:

         // 
     - linux
         // 
     - win
         //  - older Mac OS
    

    Cheers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算