dongrong1856 2015-01-24 10:17
浏览 45

更快的方法来替换特定的图像链接XPath / Preg_replace

I have written a small piece of code that loads HTML content. Processes each image and checks if it has a full URL set.

If not, it adds it.

Currently I am doing this with XPath. But I get the feeling that the procedure gets quite CPU intensive. Hence I would like to know if there would be a faster method for what I am doing currently.

The code I have written

        ///######## START A NEW DOM DOCUMENT
        $dom            = new DOMDocument;
        ///######## LOAD THE HTML INTO THE DOM DOCUMENT
        $dom->loadHTML($html);
        ///######## GET ALL IMAGES WITH XPath
        $Images         = $dom->getElementsByTagName('img');
        ///######## SETUP AN EMPTY SOURCE AND TARGET ARRAY
        $SourceArray    = $TargetArray = array();

        ///######## RUN THROUGH ALL SET IMAGE NODES
        foreach ($Images as $node) {
            ///######## GET THE URL ATTRIBUTE
            $SetURL = $node->getAttribute('src').PHP_EOL;

            ///######## IF THE URL DOES NOT START WITH HTTP
            if(substr($SetURL, 0, 4) != 'http'){
                ///######## SET THE URL TO THE SOURCE ARRAY
                $SourceArray[] = 'img src="'.$SetURL;
                ///######## SET THE TARGET URL
                $TargetArray[] = 'img src="'.__APPBASE__.'/'.$this->TEMPLATEDIR.'/'.$SetURL;
            }
        }
        ///######## REMOVE THE BREAKS FROM BOTH ARRAYS
        $SourceArray    = str_replace(PHP_EOL, NULL, $SourceArray);
        $TargetArray    = str_replace(PHP_EOL, NULL, $TargetArray);
        ///######## REPLACE THE HTML TEXT
        $html           = str_replace($SourceArray, $TargetArray, $html);

        ///######## UNSET THE VARIABLES {free up memory}
        unset($dom);
        unset($SourceArray);
        unset($TargetArray);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料