我一直在研究一个小的脚本,该脚本旨在将整个网页打包到一个html文件中。我整天都在努力,到目前为止,所有具有适当扩展名的图像都已正确转换并插入,但是,某些没有适当扩展名的图像将被跳过。
这是我的代码
function getimages($string) {
$html = $string;
$html = str_replace("https://", "http://", $html);
$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$src = $image->getAttribute('src');
if (preg_match("|^(https?:)\/\/|i", $src)) {
$type = pathinfo($src, PATHINFO_EXTENSION);
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$data = file_get_contents($src, false, stream_context_create($arrContextOptions));
if (strlen(bin2hex($data))/2 > 1) {
if (strlen($type) < 3) {
$type="png";
}
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$image->setAttribute("src", $base64);
}
}
$image->removeAttribute("srcset");
}
$html = $dom->saveHTML();
return $html;
}
这是一个示例图片网址,将被跳过
https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSTJgMWr6TgV_3mJcF9Q9jRw6K_-oTLfRErLcKGTFXhn-pMQLJB24MhbTHt4A
这几乎完全可以按照我的要求工作,但是我对regex真的很陌生,而IDK所做的工作导致这些url无法得到处理,请以我的方式来写一行。
的PHP