How do I exactly match multiple instances of img tags? I read up a few tutorials on preg_match yet never really quite understand.
I have this as my base:
<img src="http://example.com/1.png" alt="Example" />
<img class="Class" src="http://example.com/2.jpg" alt="Example 2" />
And I did up a small like regex:
<img (src="|class="Class" src=")http://.+\.(?:jpe?g|png)" alt="
After this, I'm stuck. How do I continue to match all till the end of the both strings?
I found out about the array part on PHP website itself:
preg_match('@^(?:http://)?([^/]+)@i',
"http://www.php.net/index.html", $matches);
$host = $matches[1];
Using my code, how do I get the image URL, and the alt tag?
Thanks!
For the original question, use preg_match_all()
function to get all the matches.
For the second question ("Using my code, how do I get the image URL, and the alt tag?"), basically your regex is correct. However, I would suggest to get the whole <img>
tag first, then do another preg_match()
to get the href
and alt
attributes, since their order may vary.
$html = "<img src='test.jpg' alt='aaaaaaaaaaa!'> adfa <img src='test2.jpg' alt='aaaaaaaaaaa2'> ";
$pattern = '/<img\s[^>]*>/';
$count = preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
echo "Found: " . $count . "
";
if ($count > 0) {
foreach ($matches as $match) {
$img = $match[0];
echo "img: " . $img . "
";
if (preg_match("/src=['\"]([^'\"]*)['\"]/", $img, $val)) { # UPDATE: use () to catch the content of src
$src = $val[1]; # UPDATE: get the part in ()
}
if (preg_match("/alt=['\"]([^'\"]*)['\"]/", $img, $val)) { # UPDATE
$alt = $val[1]; # UPDATE
}
echo "src = " . $src . ", alt = " . $alt . "
";
}
}
UPDATE
Answer to your comment. Sure. Just use a group to catch the part after src=
. I updated the source above and commented with "UPDATE".
Why not DOMDocument
? You can get all attributes no matter how the images are written:
$string = '<img class="Class" src="http://example.com/2.jpg" alt="Example 2" />';
$dom = new DOMDocument;
$dom->loadHTML($string);
$xpath = new DOMXPath($dom);
$query = '//img';
$elements = $xpath->query($query);
$attributes = array();
$i = 0;
foreach($elements as $one){
foreach($one->attributes as $att){
$attributes[$i][$att->nodeName] = $att->nodeValue;
}
$i++;
}
print_r($attributes);
/*Array
(
[0] => Array
(
[class] => Class
[src] => http://example.com/2.jpg
[alt] => Example 2
)
)*/