preg_match_all返回数组

时间:2015-03-02 15:58:50

标签: php preg-match preg-match-all

我最近制作了一个小脚本来捕获基于表单提交通过textarea的任何URL。

使用正则表达式是:

'/([\w]+).(local|test|stage|live).site.example.com/'

如果提交:

<p>body</p> <p>uk2.local.site.example.net 
training.test.site.example.net</p>
<p>www.google.com</p>
<p>sd2.test.site.example.net</p>

我返回的数组包含:

0   =>  array(3
  0   =>    uk2.local.site.example.net
  1   =>    training.test.site.example.net
  2   =>    sd2.test.site.example.net
)
1   =>  array(3
  0   =>    local
  1   =>    test
  2   =>    test
)

我不知道为什么我会得到第二个阵列,并且想要清理它。

1 个答案:

答案 0 :(得分:2)

使用非捕获组,也可以逃避点:

 '/(\w+)\.(?:local|test|stage|live)\.site\.example\.com/'
 // here __^^
相关问题