Search code examples
phpregexpipebracketscurly-braces

PHP Regex: Extracting content from piped curly braces


I'm trying to extract and replace Wikipedia curly braces content with no success.

In the string below, I'd like to be able to replace {{Nihongo|Pang|パン|Pan}} by Pang

$text = "Buster Bros, also called {{Nihongo|Pang|パン|Pan}} and {{Nihongo|Pomping World|ãƒãƒ³ãƒ”ング・ワールド|Ponpingu WÄrudo|lead=yes}}, is a cooperative two-player arcade video game released in 1989 by Capcom";

I tried many combinations of regex in my preg_replace such as the one below with no luck so far

$text = preg_replace('/\{\{({^:\|\}}+)\|({^:\}}+)\}\}/', "$2", $text);

Solution

  • Your question is not stated clearly.

    If you are only wanting to replace the first occurrence of curly braces in your specific data with the second element in that group, you could use a negative lookahead to match the following comma.

    $text = preg_replace('/{{[^|]*\|([^|]++)\|[^{}]++}}(?!,)/', '$1', $text);
    

    Outputs..

    Buster Bros, also called Pang and {{Nihongo|Pomping World|ãƒãƒ³ãƒ”ング・ワールド|Ponpingu WÄrudo|lead=yes}}, is a cooperative two-player arcade video game released in 1989 by Capcom
    

    If you want to replace every occurrence of curly braces with the second element in that group.

    $text = preg_replace('/{{[^|]*\|([^|]++)\|[^{}]++}}/', '$1', $text);
    

    Outputs..

    Buster Bros, also called Pang and Pomping World, is a cooperative two-player arcade video game released in 1989 by Capcom