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 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划