我有一个字符串
$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.";
Run Code Online (Sandbox Code Playgroud)
我想删除{{}}内的所有内容以及括号内的所有内容
我期待这样
$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.";
Run Code Online (Sandbox Code Playgroud)
如果这些括号不能嵌套,那很简单:
$result = preg_replace('/\{\{.*?\}\}\s*/s', '', $subject);
Run Code Online (Sandbox Code Playgroud)
如果可以的话,你需要一个递归的正则表达式:
$result = preg_replace('/\{\{(?:(?:(?!\{\{|\}\}).)*+|(?R))+\}\}\s*/', '', $subject);
Run Code Online (Sandbox Code Playgroud)
说明:
{{ # 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
180 次 |
| 最近记录: |