How can i match all images starting with pics.domain.com
?
what i've tried
preg_match_all('/<img .*src=(pics.domain.com*)["|\']([^"|\']+)/i', $row['story'], $matches);
Use DOMDocument
and simply iterate over each <img>
tag; then use parse_url()
to find the host of each image path:
$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc->loadHTML($row['story']);
libxml_clear_errors();
foreach ($doc->getElementsByTagName('img') as $img) {
if (parse_url($img->getAttribute('src'), PHP_URL_HOST) === 'pics.domain.com') {
echo "Yay, image found\n";
}
}
I've used the regex in the past, it works outside of <img>
tags as well.
'@[\'"](https?://)?([^\.][^\'"]*?)(/)?([^\'"/]*?)\.(jpg|jpeg|png|gif|bmp)[\'"]@'
A more specific version:
'@[\'"](https?://)?pics\.domain\.com[^\'"]*?\.(jpg|jpeg|png|gif|bmp)[\'"]@'
In English:
[start quote](http or https or neither)pics.domain.com(anything that isn't a quote)(some image extension)[end quote]