数据看起来像这样
cityID=123456789&sharing=blahblahblah
正在做
$cityID = preg_grep("/cityID=.\d\&$/", $sometext);
print_r($cityID);
当前打印数组()
我希望它打印 123456789
问题是$
标记行尾,因为这种模式不一定在行尾。也\d
不允许在&符号之前有一个以上的数字,所以我添加了一个+
. (另外,请注意.
匹配任何字符;不清楚这就是你想要的,这就是我在上面问的原因。)
这应该适合您:
preg_match("/cityID=\d+&/", $input_line, $output_array);
要对这种模式进行更多试验,请访问http://www.phpliveregex.com/p/1WH
你可以使用preg_match_all()
$str = "cityID=123456789&sharing=blahblahblahcityID=123456789&sharing=blahblahblahcityID=123456789&sharing=blahblahblah";
// or
// $str = "cityID=123456789&sharing=blahblahblah
// cityID=123456789&sharing=blahblahblah
// cityID=123456789&sharing=blahblahblah";
$result = preg_match_all("/cityID=(\d+)/", $str, $matches);
print_r($matches[1]);
输出:
Array ( [0] => 123456789 [1] => 123456789 [2] => 123456789 )