Punit Gajjar
Punit Gajjar

Reputation: 4987

preg_match_all not matching all possibilities

I'm trying to retrieve all matches however I am only receiving one

Here is my string

$html = '<p> This is my Home Page.</p><p><span style="line-height: 1.42857;">{{ type="slider" }} </span></p><p> </p>';

If you see string contain {{ type="slider" }} , now if i write this string only once i am getting my expected result, but if i write it multiple times in html sting like {{ type="slider" }} {{ type="banned" }} {{ type="testimonial" }}

$html = '<p> This is my Home Page.</p><p><span style="line-height: 1.42857;">{{ type="slider" }} {{ type="banner" }} {{ type="testimonial" }}  </span></p><p> </p>';

and try to get the values within my string {{ type=" ???? " }} it shows weird result

I am using this below code .

preg_match_all('/{{ type=\"(.+)\" }}/', $html, $matches, PREG_SET_ORDER);
echo "<pre>";
print_r($matches);
foreach ($matches as $val) {
    echo "matched: ". $val[0] . "<br/>";
    echo "My Value" . $val[1] . "<br/>"; 
}

Current Result :

Array
(
    [0] => Array
        (
            [0] => {{ type="slider" }} {{ type="banner" }} {{ type="testimonial" }}
            [1] => slider" }} {{ type="banner" }} {{ type="testimonial
        )

)
matched: {{ type="slider" }} {{ type="banner" }} {{ type="testimonial" }}
My Value : slider" }} {{ type="banner" }} {{ type="testimonial

I am expecting result in array with the values written between {{ type=" and " }}

with {{ type="slider" }} only i am getting this result which is perfect.

Array
(
    [0] => Array
        (
            [0] => {{ type="slider" }}
            [1] => slider
        )

)
matched: {{ type="slider" }}
My Value : slider

Any idea ?

Sorry for my bad english.

Upvotes: 1

Views: 1395

Answers (2)

cFreed
cFreed

Reputation: 4474

What you're currently getting is pretty normal, since by default your regexp is greedy, i.e. /{{ type="(.+)"}} / looks for the longest string beginning with {{ type=" and ending with }}.

Another answer here suggests you to add a "not-greedy" quantifier ? and it'll work but it's not the best solution (because it'll need more work from the regex engine).

Instead you'd better to merely replace (.+) by ([^"]+) in your regexp.

Upvotes: 1

Mike
Mike

Reputation: 24383

You need to make your regex match non-greedy by adding either a ? :

preg_match_all('/{{ type=\"(.+?)\" }}/', $html, $matches, PREG_SET_ORDER);

or the U modifier:

preg_match_all('/{{ type=\"(.+)\" }}/U', $html, $matches, PREG_SET_ORDER);

Upvotes: 6

Related Questions