dousong5492 2014-01-14 14:44 采纳率: 100%
浏览 30

PHP - 以字符串/ DOM的形式查找和修改图像链接

I have buffered page via ob_get_contents to $content variable. This content is loaded properly.

Now, from this string var I would like to extract all links related to images (accepting single and double quotes and format .png, .jpg, .jpeg., .gif, etc.) and check if these links have external path (http://server.com/path/). If true, save these files and replace external path to local (keep filename).

I tried simpliest way -> new DOCDocument - loadHTML($content), but I cant load DOM with proper (utf8) encoding (buffered string $content has proper charset, DOM no). I also tried html_entity, mb_encoding..., ->encoding('utf-8') etc. No effect.

$doc = new DOMDocument();
$doc->loadHTML($content);
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) { .... };

Then I tried regex way (preg_match_all, str_replace, etc.), but needed regex formula is too diffucult for me. Could you advice me which way is best and how to make it? Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题