dswqz24846 2015-05-15 23:21
浏览 61

PHP DOM替换标记src不起作用

I have been trying to get the HTML of an external website and look through it with PHP. I want to show images from the site, so I tried changing img sources from /assets/img/example.png to www.example.com/assets/img/example.png by getting the current src attribute like so (after getting each image from the document):

$current_src = $img->getAttribute("src");

and then assigning the current_src to the image along with the main URL. But this puts the new image in <noscript> tags like this:

<img data-cfsrc="http://www.example.com"><noscript><img src="/assets/img/example.png></noscript>

EDIT: A simple example of what I am doing is here (just for the first image):

$test_img = $doc->getElementsByTagName('img')[1]; $test_img->setAttribute("src", "http://www.example.com" . $test_img->getAttribute("src")); echo $test_img->getAttribute("src");

And this echoes http://www.example.com/assets/img/logo-bar.png, but when I go back to the view source I see that it is in a seperate tag like this:

<img style="display:none;visibility:hidden;" data-cfsrc="/assets/img/logo-bar.png" alt="example Logo"><noscript><img src="http://www.example.com/assets/img/logo-bar.png" alt="example Logo"></noscript>

What causes this? And how can I prevent this from happening?

  • 写回答

1条回答 默认 最新

  • dongyoucha0645 2015-05-15 23:26
    关注

    You should use '.' to combin a string (www.example.com) with $current_src.

    $current_src = $img->getAttribute("src"); // /assets/img/example.png
    $new_current_src = 'www.example.com'.$current_src;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多