2017-02-02 00:34
浏览 190


    $nomadspage = "";    
    $html = file_get_contents($nomadspage);
    $count = preg_match_all('/<a href="([^"]+)">[^<]*<\/a>/i', $html, $files);

    unset($files[1]); //deletes repeat array from preg_match
    $files = $files[0]; //deletes container array from preg_match

    foreach ($files as $key => $value) {
        if (substr($value, 0, 3) !== "gfs") {


I have an array with file names from an HTTP directory. I want to filter these files names so that all of the files that don't start with the three letters gfs are deleted from the array. However, for some reason, the substr() function does not work. It does not pull a substring from the file names. Therefore, the if statement does not work. Anybody know why this is happening and how to fix it?

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

  $ nomadspage =“  / NCCF / COM / GFS / PROD /“;  
 $ html = file_get_contents($ nomadspage); 
 $ count = preg_match_all('/&lt; a href =“([^”] +)“&gt; [^&lt;] *&lt; \ / a&gt; / i  ',$ html,$ files); 
 unset($ files [1]); //从preg_match删除重复数组
 $ files = $ files [0]; //从preg_match删除容器数组
 \  n foreach($ files as $ key =&gt; $ value){
 if(substr($ value,0,3)!==“gfs”){
 unset($ files [$ key]); 
 var_dump($ files); 

我有一个包含HTTP目录文件名的数组。我想过滤这些文件名 以便从数组中删除所有不以三个字母 gfs 开头的文件。但是,由于某种原因, substr()函数不起作用 它没有从文件名中提取子字符串。因此, if 语句不起作用。有人知道为什么会发生这种情况以及如何修复它吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanji5569 2017-02-02 00:46

    $files[0] contains the strings that match the entire regular expression, so substr($value, 0, 3) is always "<a ". You should set $files to $files[1], not $files[0], it contains all the matches of the ([^"]+) pattern.

    Actually, it's best not to use regular expressions to parse HTML. Use a DOM parser library, such as the DOMDocument class.

    打赏 评论

相关推荐 更多相似问题