2013-01-31 50 views
2

美好的一天。特定标记之间的preg_split值

我遇到问题模式

假设我有一个这样的字符串:

你好[人]名称[/人],我不知道正则表达式,像 [人] Another_name [/人]执行。

我需要preg_split此字符串来得到一个数组是这样的:

Array(0 => 'Name', 1 => 'Another_name'); 

我一直在试图解决这个一段时间,仍然没有运气。

赦免我的无知。任何形式的帮助,都表示感谢。

+2

使用preg_match_all而不是使preg_split。 – nhahtdh

+0

我们可以假设'[person]'的语法永远不会有任何变化(即它总是看起来像问题中所示;没有附加属性或类似的东西),并且不会有嵌套的可能性标签?如果我们可以假设,这并不难。 – SDC

+0

*另请参阅[开源RegexBuddy替代品](http://stackoverflow.com/questions/89718/is-there)和[在线正则表达式测试](http://stackoverflow.com/questions/32282/regex-testing)对于一些有用的工具,或[RegExp.info](http://regular-expressions.info/)更好的教程。 – mario

回答

4

你要使用类似preg_match_all而不是preg_split

preg_match_all("|\[person\](.*)\[/person\]|U", 
    "Hello [person]Name[/person], I don't know regex like [person]Another_name[/person] does.", 
    $out); 

echo $out[1][0] . ", " . $out[1][1] . "\n"; 

您可以了解更多有关$out是如何在这里的结构:http://www.php.net/manual/en/function.preg-match-all.php

相关问题