1

I need help to uppercase the text inside double square bracket. for example :

I want to turn below :

hi, [[i]] want [[this]] to be [[uppercase]]

Resulting as below:

hi, [[I]] want [[THIS]] to be [[UPPERCASE]]

When I use below code everything turns everything into uppercase:

$pattern = '/\[\[(.*)\]\]/';
$new_string = preg_replace_callback($pattern, 
    function($match)
    {
        return strtoupper($match[0]);
    }
    , $string);

what is missing in my code?

jmjap
  • 166
  • 13

1 Answers1

3

Your pattern is greedy it wants everything..

Change

$pattern = '/\[\[(.*)\]\]/';

to

$pattern = '/\[\[(.*?)\]\]/';

Here are some links on the topic if you need more informtation.

What do lazy and greedy mean in the context of regular expressions? http://www.rexegg.com/regex-quantifiers.html

Community
  • 1
  • 1
chris85
  • 23,846
  • 7
  • 34
  • 51