我有一个像这样的字符串
$data = "{{quickbar | image=Baby Beach Aruba.JPG | caption=Baby Beach | location=LocationAruba.png | flag=Flag of Aruba.svg | capital=Oranjestad | government=parliamentary democracy | currency=Aruban guilder/florin (AWG) | area=193 sq km | population=71,891 (July 2006 est.) | language=Dutch (official), Papiamento (a creole of Spanish, Portuguese, and Dutch origin), English (widely spoken), Spanish | religion=Roman Catholic 82%, Protestant 8%, Hindu, Muslim, Confucian, Jewish | electricity=120V/60Hz (North American plug) | callingcode=+297 | tld=.aw | timezone=UTC -4 }} Aruba [1] is a Caribbean island 15 miles north of the coast of Venezuela. The island is an autonomous dependency of the Kingdom of the Netherlands.";我要删除{{}}中的所有内容以及该括号
我期望是这样的
$data = "Aruba [1] is a Caribbean island 15 miles north of the coast of Venezuela. The island is an autonomous dependency of the Kingdom of the Netherlands.";发布于 2013-06-11 14:21:17
如果这些括号不能嵌套,那就很简单了:
$result = preg_replace('/\{\{.*?\}\}\s*/s', '', $subject);如果可以,您需要一个递归正则表达式:
$result = preg_replace('/\{\{(?:(?:(?!\{\{|\}\}).)*+|(?R))+\}\}\s*/', '', $subject);说明:
{{ # Match {{
(?: # Either match...
(?: # the following regex:
(?!{{|}}) # Unless we're at the string {{ or }},
. # match any character
)*+ # any number of times (possessively to avoid backtracking).
| # Or match...
(?R) # whatever this entire regex matches (recursively)
)+ # End of alternation, repeat as necessary
}} # Match }}
\s* # Match optional trailing whitespace请在regex101.com上查看。
发布于 2013-06-11 14:25:34
<?php
$data = "this is {{ remove }} a {{ remove }} sample {{ remove }} text";
echo $data = preg_replace("/\{\{[^}]+\}\}/", "", $data); //this is a sample text
?>发布于 2013-06-11 14:26:58
这应该是可行的
$data = "this is {{ remove }} a {{ remove }} sample {{ remove }} text";
echo preg_replace('/(\{\{)[^\{]*(\}\})/', '', $data);https://stackoverflow.com/questions/17037653
复制相似问题