<?php
header("content-Type: text/html; charset=Utf-8");
$url = "http://www.weather.com.cn/weather/101251401.shtml";
$fp = @fopen($url, "r") or die("连接超时");
$fcontent = file_get_contents($url);
eregi("<div class=\"cleardiv\"></div>(.*)<script type=\"text/javascript\" src=\"http://m.weather.com.cn/a1.js\">",$fcontent,$we);$we[1] = str_replace("src=\"http://localhost/m2/i/icon_weather/29x20/", "src=\"http://www.weather.com.cn/m2/i/icon_weather/29x20/", $we[1]);
echo $we[1];
?>我是看PHP100视频照着写的,但是图片就是显示不出来...
以下是截图:下面是天气预报网站的图片的地址
PHP

解决方案 »

  1.   

    取得的内容中,图片的连接是
    src="/m2/i/icon_weather/29x20/"
    src="http://localhost/m2/i/icon_weather/29x20/"
    所以你的替换并没有成功如果只是直接显示,那么可以配上来源在输出
    echo str_replace('</head>', '<base href="http://www.weather.com.cn"></head>', $fcontent);
    你可以明显的看到样式没有了
    改成这样才会有样式echo str_replace('<head>', '<head><base href="http://www.weather.com.cn">', $fcontent);
    通常的做法是将样式表、图片抓回到本地。当然也存在一定的技术难度
      

  2.   

    header("content-Type: text/html; charset=Utf-8");
    $url = "http://www.weather.com.cn/weather/101251401.shtml";
    $fp = @fopen($url, "r") or die("连接超时");
    $fcontent = file_get_contents($url);
    echo str_replace('<head>', '<head><base href="http://www.weather.com.cn">', $fcontent);//就加这句,其他都是你的