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 = "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.";
4

4 回答 4

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上查看。

于 2013-06-11T06:21:16.810 回答
2
<?php
$data = "this is {{ remove }} a {{ remove }} sample {{ remove }} text";
echo $data = preg_replace("/\{\{[^}]+\}\}/", "", $data); //this is a sample text
?>
于 2013-06-11T06:25:34.053 回答
1

这应该工作

$data = "this is {{ remove }} a {{ remove }} sample {{ remove }} text";
echo preg_replace('/(\{\{)[^\{]*(\}\})/', '', $data);
于 2013-06-11T06:26:58.013 回答
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);

输出:

阿鲁巴岛 [1] 是位于委内瑞拉海岸以北 15 英里的加勒比海岛屿。该岛是荷兰王国的一个自治属地。

此功能适用于任意数量的括号。

于 2013-06-11T06:40:50.600 回答