我正在尝试从css文件下载所有图像。图像采用url(https://www...image.png)
格式
<?php
$result = file_get_contents("global-61.css");
$path = 'found/';
preg_match_all('https:\/\/www.[^0-9]+.(png|jpg|jpeg|gif)', $result, $output, PREG_SET_ORDER);
foreach($output as $item)
{
copy($item, $path . basename($item));
}
?>
链接来自不同的网站 - 但我认为我的正则表达不正确。当我尝试运行脚本时,它与任何图像都不匹配。如果正则表达式没问题,这里可能有什么问题?热链接保护?
答案 0 :(得分:2)
尝试:
preg_match_all('/\bhttps:\/\/www\.[^0-9]+\.(png|jpg|jpeg|gif)\b/m'
你错过了正则表达式(/'s)的封闭分隔符,你需要通过使用\来转义它来告诉它与文字点匹配,所以它变成了上面的那些。
一些改进:
?
正如Kameleon博士建议的那样,你也可以很容易地匹配https和http
preg_match_all('/\bhttps?:\/\/(www)?\.[^0-9]+\.(png|jpg|jpeg|gif)\b/'
答案 1 :(得分:2)
一些注意事项:
http(s)?
[^0-9]
,[A-Za-z\-]+
之类的内容会更好那么,如下所示:
http(s)?:\/\/www\.[A-Za-z0-9\-]+\.(png|jp(e)?g|gif)