I have no problem grabbing images from a page with the code below but how do I modify it to grab both images AND images wrapped in an anchor?
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
I have no problem grabbing images from a page with the code below but how do I modify it to grab both images AND images wrapped in an anchor?
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
You can use something like this to grab either the whole image tag or just the image name out of the string:
$string = '<img src="http://www.google.com/trans.gif">
<a href="http://www.google.com"><img src="http://www.yahoo.com/images/placeholder.gif"></a>';
if (preg_match_all('/<img.*?src=[\'"](.*?)[\'"].*?>/i', $string, $matches)) {
print "<pre>"; print_r($matches); print "</pre>";
}
else {
print "Could not find any matches";
}
This outputs the following:
Array
(
[0] => Array
(
[0] => <img src="http://www.google.com/trans.gif">
[1] => <img src="http://www.yahoo.com/images/placeholder.gif">
)
[1] => Array
(
[0] => http://www.google.com/trans.gif
[1] => http://www.yahoo.com/images/placeholder.gif
)
)
Explanation of the REGEX:
<img .*? src= [\'"] (.*?) [\'"] .*? >
^ ^ ^ ^ ^ ^ ^ ^
1 2 3 4 5 6 7 8
<img
Look for a literal opening image tag..*?
Match any character .
, any number of times *
until it hits the next part of the expression ?
. In this case the next part of the expression is src=
, so it will stop looking for stuff once it hits that.src=
Look for the exact text of src=
.[\'"]
A character class meaning to match either a single or double quote.(.*?)
This is the same as number 2, except we put it in parenthesis so that we can capture whatever it finds.[\'"]
Same as number 4..*?
Same as number 2.>
Look for a literal greater than sign (closing HTML bracket).