phppreg-replacemarkup

Remove square brackets from wiki markup


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.


Solution

  • 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.