matching images inside a link in regex
Posted
by
user225269
on Stack Overflow
See other posts from Stack Overflow
or by user225269
Published on 2013-06-30T06:33:22Z
Indexed on
2013/06/30
16:21 UTC
Read the original article
Hit count: 158
What is wrong with regex pattern that I created:
$link_image_pattern = '/\<a\shref="([^"]*)"\>\<img\s.+\><\/a\>/';
preg_match_all($link_image_pattern, $str, $link_images);
What I'm trying to do is to match all the links which has images inside of them.
But when I try to output $link_images
it contains everything inside the first index:
<pre>
<?php print_r($link_images); ?>
</pre>
The markup looks something like this:
Array ( [0] => Array ([0] => "
<p> </p>
<p><strong><a href="url">Title</a></strong></p>
<p>Desc</p>
<p><a href="{$image_url2}"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="{$image_url2}" width="569" height="409"></a></p>
But when outputting the contents of the matches, it simply returns the first string that matches the pattern plus all the other markup in the page like this:
<a href="{$image_url}"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="{$image_url}" width="568" height="347"></a></p>
<p> </p>
<p><strong><a href="url">Title</a></strong></p>
<p>Desc</p>
<p><a href="{$image_url2}"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="{$image_url2}" width="569" height="409"></a></p>")
© Stack Overflow or respective owner