我有一个像
这样的字符串$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.";
答案 0 :(得分:6)
如果这些括号不能嵌套,那很简单:
$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上查看。
答案 1 :(得分:2)
<?php
$data = "this is {{ remove }} a {{ remove }} sample {{ remove }} text";
echo $data = preg_replace("/\{\{[^}]+\}\}/", "", $data); //this is a sample text
?>
答案 2 :(得分:1)
这应该有效
$data = "this is {{ remove }} a {{ remove }} sample {{ remove }} text";
echo preg_replace('/(\{\{)[^\{]*(\}\})/', '', $data);
答案 3 :(得分:0)
如果没有必要,请不要使用正则表达式。
$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 = implode('', array_map(function($a) { // Pass substring to array_map()
$a = explode("}}", $a); // Split substring into array at boundaries of }}
return $a[count($a) - 1]; // The last index of this array contains your content. The others contain the contents starting at {{ and ending at }}
}, explode("{{", $data))); // Split string into array of substrings at boundaries of {{
print_r($data);
输出:
Aruba [1]是加勒比岛屿,位于海岸以北15英里处 委内瑞拉。该岛是一个自治的王国 荷兰。
此功能适用于任意数量的括号。