【问题标题】:php preg_match_all pattern for parenthesesphp preg_match_all 括号模式
【发布时间】:2015-10-12 21:22:25
【问题描述】:

我试图获取两个括号 () 之间的值的每个实例。我的函数返回数组中的每个字符,而不是它应该返回的两个字符串。我猜模式中有什么东西?我一直在尝试不同的方法。任何帮助将不胜感激。

$formula = "if([Paid] === 1, 'Need To Pay', 'Paid Up')+if([Paid] === 1, 'Million', 'Good to Go')"

我的班级:

<?php

class Formula {

    public function explode_formula($formula){
        echo $this->get_string_between($formula, "(", ")");
    }

    public function get_string_between($string, $start, $end){
        preg_match_all("~((.+?))~", $string, $m);
        print_r($m[1]);
    }

}

需要返回:

[Paid] === 1, 'Need To Pay', 'Paid Up'
[Paid] === 1, 'Million', 'Good to Go'

【问题讨论】:

  • 您必须转义文字 \( 括号才能让正则表达式找到它们。

标签: php arrays preg-match-all


【解决方案1】:

通过转义外括号来更改您的正则表达式行:

From: preg_match_all("~((.+?))~", $string, $m);
To:   preg_match_all("~\((.+?)\)~", $string, $m);

参考: http://php.net/manual/en/regexp.reference.escape.php

【讨论】:

    猜你喜欢
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    相关资源
    最近更新 更多