[英]Using preg_match to find all text between brackets
I am trying to search a document for tags, to later replace them. 我正在尝试在文档中搜索标签,以便以后替换它们。
I am using preg_match, but am having some difficulty. 我正在使用preg_match,但遇到了一些困难。
preg_match('/\[.*\]/', $haystack, $matches);
Searching through the following text 搜索以下文本
[TODAY_DATE] [TODAY_DATE]
Re: Demand for Payment 回复:付款要求
[ADDRESS] [ADDRESS_LINE_2] ...etc [ADDRESS] [ADDRESS_LINE_2] ...等等
print_r($matches);
returns 回报
Array ( [0] => [TODAY_DATE] )
How should I adjust my regex to return all matches? 我应该如何调整我的正则表达式以返回所有匹配项?
Use preg_match_all
. 使用preg_match_all
。 As the name suggests, it matches more than once. 顾名思义,它匹配多次。
preg_match_all('/\[.*?\]/', $haystack, $matches);
var_dump($matches);
Also use reluctant quantifier .*?
还使用勉强的量词.*?
instead of greedy one. 而不是贪婪的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.