[英]preg_match_all only return the first array with index 0 and not one with index 1
1 . 我正在嘗試將 from 中的一些 ID 與 url 匹配並返回所有 ID。
代碼如下。
字符串是這樣的
"gfdgfds.php?id=67f8d4fd6d4&gfddgfds.php?
id=67f8d4f6d4&gfddgfds.php?id=67f8d4f6d4&gfdsgfds.php?id=67f8d4f6fdd4&"
圖案
$pattern = '/p?id=[0-9A-Za-z]*/';
代碼
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.');
}
但是從上面當我做 var_dump($matches) 我得到。
array (size=1)
0 =>
我期待得到
array (size=1)
0 =>array()
1 =>array() // So I can use this.
我在這里缺少什么,謝謝。
2 . 另外,我希望 $pattern = "'/p?id=[0-9A-Za-z]*/'" 匹配 "php?id=value",而不僅僅是任何 id=value。
相反,使用正則表達式,你可以用parse_url
功能,但不知道到底什么是你期望的輸出,但不是preg_match_all
使用preg_match
與以下的正則表達式
(.php\?id=(\w+))
所以你的代碼看起來像
preg_match("/(.php\?id=(\w+))/",$your_string,$matches);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.