2

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

2 回答 2

4

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";
    }
}
于 2013-04-10T02:48:23.220 回答
2

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]

于 2013-04-10T02:41:59.697 回答