简体   繁体   English

preg_match_all 只返回索引为 0 的第一个数组,而不返回索引为 1 的数组

[英]preg_match_all only return the first array with index 0 and not one with index 1

1 . 1 . I'm trying to match some IDs from a from with url and return all IDs.我正在尝试将 from 中的一些 ID 与 url 匹配并返回所有 ID。

The code is as follow.代码如下。

The string is something like this字符串是这样的

"gfdgfds.php?id=67f8d4fd6d4&gfddgfds.php?
id=67f8d4f6d4&gfddgfds.php?id=67f8d4f6d4&gfdsgfds.php?id=67f8d4f6fdd4&"

Pattern图案

$pattern = '/p?id=[0-9A-Za-z]*/';

CODE代码

preg_match_all($pattern,$input,$matches);   
    //var_dump(count($matches));
     if( count($matches) > 0 ) {
         var_dump($matches);
        //return $matches[1];
     } else {
        die('No matches found.');    
    }

But from the above when I do var_dump($matches) I get.但是从上面当我做 var_dump($matches) 我得到。

array (size=1)
  0 => 

And I was expecting to get我期待得到

array (size=1)
      0 =>array()
      1 =>array() // So I can use this.  

What am I missing here, thanks.我在这里缺少什么,谢谢。

2 . 2 . Also, I'd like the $pattern = "'/p?id=[0-9A-Za-z]*/'" to match "php?id=value", not just any id=value.另外,我希望 $pattern = "'/p?id=[0-9A-Za-z]*/'" 匹配 "php?id=value",而不仅仅是任何 id=value。

Instead of using regex you can use parse_url function but don't know exactly what is your expected output but instead of preg_match_all use preg_match with following regex相反,使用正则表达式,你可以用parse_url功能,但不知道到底什么是你期望的输出,但不是preg_match_all使用preg_match与以下的正则表达式

(.php\?id=(\w+))

So your code looks like as所以你的代码看起来像

preg_match("/(.php\?id=(\w+))/",$your_string,$matches);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM