如何捕获括号内的文本
我有这个小代码来捕获括号之间的文本:
$string = '[title=my title] http://bit.ly/gANFxi [maps:some cool place]';
preg_match_all('/\[([^\]]*)\]/', $string, $matches);
print_($matches[0]);
我得到的是以下内容:
Array( [0] => [title=my title] [1] => [maps:some cool place] )
我想做出更多限制以避免“[some-not Cool]”或“[another+stuff]”, 所以我只需要捕获 [some:thing] 和 [some=thing]。
我该怎么做呢?
I have this little code to capture text between brackets:
$string = '[title=my title] http://bit.ly/gANFxi [maps:some cool place]';
preg_match_all('/\[([^\]]*)\]/', $string, $matches);
print_($matches[0]);
What I get is the following:
Array( [0] => [title=my title] [1] => [maps:some cool place] )
I want to make more restrictive to avoid "[some-not cool]" or "[another+stuff]",
so I need to catch only [some:thing] and [some=thing].
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将捕获包含“=”或“:”的所有内容并忽略其他内容:
这可以解决问题吗?或者还有其他要求吗?也就是说,这会返回“[some stuff=some:thing]”,但是应该吗? (注意多个单词以及“=”和“:”)。
This will catch everything that contains a '=' or ':' and ignore others:
Does this do the trick? Or are there further requirements? That is, this return "[some stuff=some:thing]" but should it? (note both the multiple words and both '=' and ':').
如果冒号或等号之前的部分仅由字母组成,则可以使用以下代码:
如果第一部分更类似于 PHP 标识符($ 除外),则您可以使用以下代码:
If the part before the colons or the equal should be made only of letters, you can use the following code:
If the first part is something more similar to a PHP identifier (except for the $), then you can use the following code: