让我说我有这个:
$id = "bla bla 123 number/4567''' ";
然后,我想添加一个仅过滤数字4567的preg_match。$ id中唯一稳定的东西是number / xxxx,所以当它找到这个结构编号/ xxxx或number / xxxxxxxx时(无关紧要)许多数字都是,将这些数字引入一个名为$ a的var。
示例:
input: $id = "bla bla 434323 123 number/456789abc ";
In this case $a will take the value 456789
但是,这种情况怎么样:
input: $id = "bla bla number/123 bla bla number/456 bla bla number/789xx ";
in this situation I want to get all matches like this:
$a will be 123
$b will be 456
$c will be 789
这不起作用:
<?php
$id = "bla bla number/123 number/456789abc ";
function find_in_id_content($id)
{
if ( preg_match_all('#(?<=number/)\d+#', $str, $m ) ) {
list($a, $b, $c, $d) = $m[0];
printf("a=%s,b=%s,c=%s\n", $a, $b, $c);
}
}
echo $a;
?>
答案 0 :(得分:1)
您可以使用:
if ( preg_match_all('#(?<=number/)\d+#', $str, $m ) ) {
list($a, $b, $c, $d) = $m[0];
printf("a=%s,b=%s,c=%s\n", $a, $b, $c);
}
a=123,b=456,c=789
答案 1 :(得分:0)
preg_match_all('/number\/([0-9]+)/', $id, $matches);