【问题标题】:php separate strings with delimitersphp用分隔符分隔字符串
【发布时间】:2014-02-10 08:43:58
【问题描述】:

我有字符串例如:

$stringExample = "(({FAPAGE15}+500)/{GOGA:V18})"
// separete content  { }   

我需要这样的结果::

$response = array("FAPAGE15","GOGA:V18")  

我认为它必须是:preg_splitpreg_match

【问题讨论】:

    标签: regex


    【解决方案1】:

    这是您需要的正则表达式:

    \{(.*?)\}
    

    正则表达式示例:

    http://regex101.com/r/qU8eB0

    PHP:

    $str = "(({FAPAGE15}+500)/{GOGA:V18})";
    
    preg_match_all("/\{(.*?)\}/", $str, $matches);
    
    print_r($matches[1]);
    

    输出:

    Array
    (
        [0] => FAPAGE15
        [1] => GOGA:V18
    )
    

    工作示例:

    https://eval.in/92516

    【讨论】:

      【解决方案2】:

      您可以使用否定字符类:[^}] (所有不是}

      preg_match_all('~(?<={)[^}]++(?=})~', $str, $matches);
      
      $result = $matches[0];
      

      模式细节

      ~         # pattern delimiter
      (?<={)    # preceded by {
      [^}]++    # all that is not a } one or more times (possessive)
      (?=})     # followed by }
      ~         # pattern delimiter
      

      注意:所有格量词++ 不是获得好的结果所必需的,可以用+ 代替。您可以找到有关此功能的更多信息here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-31
        • 1970-01-01
        相关资源
        最近更新 更多