Punit Gajjar Punit Gajjar - 3 months ago 9
PHP Question

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.

Answer

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);
Comments