regex to find string within square brackets []

I want to capture the text within the square brackets in the html string below.
But the regex I have below doesn’t get ‘image’ and imagealt’ seperately but returns ‘image]” alt=”[imagealt’ instead. If I take out the alt=”[imagealt]” from the string it returns as I would expect/want.

$html = '<h2>[title]</h2>
<div class="content"><img src="[image]" alt="[imagealt]" /></div>
<div class="content">[text]</div>';

preg_match_all("^\[(.*)\]^",$html,$fields, PREG_PATTERN_ORDER);

echo "<pre>";
echo "</pre>";

    [0] => Array
            [0] => [title]
            [1] => [image]" alt="[imagealt]
            [2] => [text]

    [1] => Array
            [0] => title
            [1] => image]" alt="[imagealt
            [2] => text


Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

your regex is being greedy. you need to stop it being greedy to do what you want. Find out a bit more about greediness here.

When a match is greedy it will ignore the first situation which satisfies the regex and will keep trying to match until it consumes as much of the input as it can.

Usually this involves adding a ? but I’m not certain in php, but you could try:

preg_match_all("^\[(.*?)\]^",$html,$fields, PREG_PATTERN_ORDER);

Solution 2

preg_match_all("#\[[^\]]*\]#",$html,$fields, PREG_PATTERN_ORDER);

The ^ caret is used for mark the start of the string, so I use # or | for the delimiter to avoid confusion. Also, I use [^\]*] instead of .*? because it is certain to stop once it hits the end of the ], whereas you need the lazy modifier on your . and maybe even the m modifier to ensure it captures line breaks if your attributes decide to include them.

Solution 3


     preg_match_all("^\[(.*?)\]^",$html,$fields, PREG_PATTERN_ORDER);

The extra ? means “non greedy match” it will stop after a ] is found

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply