【问题标题】:How can I make preg_match_all array [duplicate]如何制作 preg_match_all 数组[重复]
【发布时间】:2013-07-27 03:07:22
【问题描述】:

我有这个:

$text = $_POST['text'];

当我echo $text 我得到这个:

ggg #hhh #ddd ggg hhhrr ggg #ttt 

当我这样做时:

$val = preg_match_all("/#\w+/", $text, $matches);
print_r($matches);

我明白了

Array ( [0] => Array ( [0] => #hhh [1] => #ddd [2] => #ttt ) )

但我想要这样的输出:

Array ( [0] => #hhh [1] => #ddd [2] => #ttt ) 

谢谢

【问题讨论】:

    标签: php regex hashtag


    【解决方案1】:

    通过改变这个从数组中取出第一个(零)项:

    print_r($matches);
    

    到这里:

    print_r($matches[0]);
    

    【讨论】:

      【解决方案2】:

      另一种方法是使用命名组。

      $val = preg_match_all("/(?P<myLabel>#\w+)/", $text, $matches);
      print_r($matches['myLabel']);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-30
        • 2016-10-24
        • 1970-01-01
        • 2013-05-06
        相关资源
        最近更新 更多