繁体   English   中英

php preg_match_all没有得到所有结果

[英]php preg_match_all Not getting all results

我正在使用php preg_match_all来提取消息的某些部分,如下所示:

$customerMessage = '"message":"success:2,2;3,3;"' ;
preg_match_all('/("message":")([a-z0-9A-Z]+):([0-9]+,[0-9]+;)+/', $customerMessage, $matches);
var_dump($matches);
die;

这段代码输出是:

array(4) {
  [0]=>
  array(1) {
    [0]=>
    string(27) ""message":"success:2,2;3,3;"
  }
  [1]=>
  array(1) {
    [0]=>
    string(11) ""message":""
  }
  [2]=>
  array(1) {
    [0]=>
    string(7) "success"
  }
  [3]=>
  array(1) {
    [0]=>
    string(4) "3,3;"
  }
}

为什么我不能参加2,2; 提前致谢!

您只能获得组的最后一场比赛。 两个得到所有的值,如x,x; 你可以使用你当前的正则表达式,改变一下:

preg_match_all('/("message":")([a-z0-9A-Z]+):(.*)"/', $customerMessage, $matches);
/* $matches[3] --> 2,2;3,3;

现在你可以使用$matches[3]获得第3组并匹配所有x,x; [0-9]+,[0-9]+;

preg_match_all('/[0-9]+,[0-9]+/', $matches[3], $matches2);
/* $matches[0] --> 2,2;
/* $matches[1] --> 3,3;

暂无
暂无

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

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