Home > Back-end >  Remove square brackets from wiki markup
Remove square brackets from wiki markup

Time:07-08

I'm looking for the way to remove some markup from wikitext.

Example:

Mexico, officially the United Mexican States, is a [wiki=1c1ff8db21cf79377f9930e6e6ff8247]country[/wiki] in the southern portion of [wiki=5ffec2d87ab548202f8b549af380913a]North America[/wiki].

Returned text should be:

Mexico, officially the United Mexican States, is a country in the southern portion of North America.

What we tried:

preg_replace('/(\[.*?.\])/', '', $txt)

Thanks.

CodePudding user response:

Modify the pattern to replace the text between the open and close tags.

$markup = 'Mexico, officially the United Mexican States, is a [wiki=1c1ff8db21cf79377f9930e6e6ff8247]country[/wiki] in the southern portion of [wiki=5ffec2d87ab548202f8b549af380913a]North America[/wiki].';
$plain = preg_replace('/\[.*?\](.*?)\[\/.*?\]/', '$1', $markup);
echo $plain;

Output

Mexico, officially the United Mexican States, is a country in the southern portion of North America.

CodePudding user response:

If you want to match the wiki: you can use:

\[wiki=[^][]*](. ?)\[/wiki]

Explanation

  • \[wiki= Match [wiki=
  • [^][]* Optionally repeat matching any char except [ or ]
  • ] Match the closing square bracket
  • (. ?) Capture group 1, match 1 chars as least as possible
  • \[/wiki] Match [/wiki]

In the replacement use group 1 like $1

Regex demo

Example

$re = '`\[wiki=[^][]*](. ?)\[/wiki]`m';
$str = 'Mexico, officially the United Mexican States, is a [wiki=1c1ff8db21cf79377f9930e6e6ff8247]country[/wiki] in the southern portion of [wiki=5ffec2d87ab548202f8b549af380913a]North America[/wiki].
';

$result = preg_replace($re, '$1', $str);

echo $result;

Output

Mexico, officially the United Mexican States, is a country in the southern portion of North America.
  • Related