【问题标题】:php preg_match_all Not getting all resultsphp preg_match_all 没有得到所有结果
【发布时间】:2016-11-29 12:46:27
【问题描述】:

我正在使用 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; ? 提前致谢!

【问题讨论】:

    标签: php regex preg-match-all


    【解决方案1】:

    您只能获得小组的最后一场比赛。两个获取所有值,例如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;
    

    【讨论】:

    • 你只能得到一个小组的最后一场比赛!谢谢。你是对的!
    猜你喜欢
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多