php从HTML中获取所有图片
从HTML中获取所有图片。
应用案例
$txt='<p> 最近访问站点:实时显示正在使用豫唐主题或者插件的站点。 </p> <p style="text-align: center;"> <img class="ue-image" src="https://www.ytecn.com/zb_users/upload/2022/03/202203201647760489897832.png" title="豫唐商务服务平台大数据中心" alt="豫唐商务服务平台大数据中心"/> </p>'; $array=RemovePHPCode($txt); print_r($array);
输出结果为:
Array ( [0] => https://www.ytecn.com/zb_users/upload/2022/03/202203201647760489897832.png )
函数
function GetImagesFromHtml($html) { $pattern = "/<img[^>]+src=[\\'|\"](.*?)[\\'|\"][^>]*>/i"; //$pattern = '/<img[^>]+src="([^">]+)"[^>]*>/i'; //沉水 preg_match_all($pattern, $html, $matches); $array = is_array($matches[1]) ? $matches[1] : array(); foreach ($array as $key => $value) { $array[$key] = htmlspecialchars_decode($array[$key]); } $array = array_unique($array); return $array; }