FrancisMV123
FrancisMV123

Reputation: 3449

How to remove content inside {{ }} from string - PHP

I have a string like

$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.";

I want to remove all contents inside {{}} and that brackets also

I am expecting like this

$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.";

Upvotes: 0

Views: 193

Answers (4)

Tim Pietzcker
Tim Pietzcker

Reputation: 336498

If those brackets can't ever be nested, it's simple:

$result = preg_replace('/\{\{.*?\}\}\s*/s', '', $subject);

If they can, you need a recursive regex:

$result = preg_replace('/\{\{(?:(?:(?!\{\{|\}\}).)*+|(?R))+\}\}\s*/', '', $subject);

Explanation:

{{          # 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

See it on regex101.com.

Upvotes: 6

George Reith
George Reith

Reputation: 13476

Don't use regex if it isn't necessary.

$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);

Outputs:

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.

This function will work for any number of those brackets.

Upvotes: 0

Meiko Watu
Meiko Watu

Reputation: 481

This should work

$data = "this is {{ remove }} a {{ remove }} sample {{ remove }} text";
echo preg_replace('/(\{\{)[^\{]*(\}\})/', '', $data);

Upvotes: 1

user1646111
user1646111

Reputation:

<?php
$data = "this is {{ remove }} a {{ remove }} sample {{ remove }} text";
echo $data = preg_replace("/\{\{[^}]+\}\}/", "", $data); //this is a sample text
?>

Upvotes: 2

Related Questions