【问题标题】:String problem in PHPPHP中的字符串问题
【发布时间】:2009-12-14 16:36:45
【问题描述】:

如何取出字符串中的某个字符并将它们全部放在一个数组中,例如:

"{2} in better that {1} when it comes to blah blah blah"

输出将是:

array(0 => "2", 1 => "1");

我使用了正则表达式,但它似乎没有在整个字符串中循环,或者我可能遗漏了什么?

谢谢

【问题讨论】:

    标签: php arrays string


    【解决方案1】:

    使用preg_match_all 代替 preg_match:

    <?php
    $str = "{2} in better that {1} when it comes to blah blah blah";
    preg_match_all('/{\d+}/', $str, $matches);
    print_r($matches[0]);
    ?>
    

    在我的机器上显示:

    Array
    (
        [0] => {2}
        [1] => {1}
    )
    

    【讨论】:

      【解决方案2】:
      preg_match_all('/\{\d+\}/', $yourString, $matches);
      
      var_dump($matches);
      

      【讨论】:

        猜你喜欢
        • 2014-10-04
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        • 2012-02-08
        • 2013-05-28
        • 2015-09-26
        • 2019-11-17
        • 1970-01-01
        相关资源
        最近更新 更多