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);

?>

图片转代码服务由CSDN问答提供 功能建议

我想只使用每个文件中的文件名进行进一步编码。 我怎样才能做到这一点。 我使用substr但它只在第一行工作。 数组$ S2成立:

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

我只想要'|'之前的文字 : -

 <代码> access.2018.08.09.log 
access.2018.08.12.log 
access.2018.08.13.log 
   \  n 
 

代码: -

 &lt; pre&gt;&lt;?php 
 
 $ files = scandir('C:\ wamp64 \ www \ MyLogs \  logsfiles'); 
foreach($ files as $ key =&gt; $ 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); 
 
?&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    点赞 评论

相关推荐 更多相似问题