phppreg-replaceshortcodeplaceholderquantifiers

Using preg_replace to modify square brace placeholders in a string is incorrectly matching multiple placeholders at once


I need to remove an attribute declaration expression inside of a square-braced shortcode. This is my code:

$pattern = "#\[(.*)( rep=['\"]{1}1['\"]{1})(.*)\]#i";
$replacement = '[$1$3]';

//Test cases:
$values = [
  'This is a [s1 val="1"] test',
  'This is a [s1 val="1"] test rep="1"',
  'This is a [s1 val="1" rep="1"] test',
  'This is a [s1 de="2" rep="1" be="2"] test',
  'This is a [rep="1" de="2" ] test',
  'This is a [s2 val="1" rep="1" de="2"] test ... This is a [s3 val="1" rep="1" de="2" ] test',
];

foreach($values as $value)
{
    echo preg_replace($pattern, $replacement, $value) . "\n";
}

The first five cases work as expected. Unfortunately, the sixth case which has two shortcodes with rep="1" inside doesn't work as expected. I want the rep="1" in both shortcodes removed. Something must be wrong with the regular expression.

NOTE: the rep="1" must be inside the shortcode (inside the [ and ]) for it to be replaced.


Solution

  • You need to place the non-greedy quantifier within your regex

    \[(.*?)(rep="1")(.*?)\]
       //^^          //^^
    

    Demo