php采集某个网页的内容,并把图片本地化

        <pre class="prettyprint lang-php">&lt;?php 

// 获取网页标题和内容 $url = 'http://www.example.com'; $html = file_get_contents($url); $title = preg_match('/<title>(.?)<\/title>/', $html, $matches); $title = $matches[1]; $content = preg_match('/<div id="content">(.?)<\/div>/', $html, $matches); $content = $matches[1];

// 把图片资源本地化 $img_pattern = '/<img.?src="(.?)"/'; preg_match_all($img_pattern, $html, $img_matches); $img_urls = $img_matches[1]; foreach ($img_urls as $img_url) { $img_name = basename($img_url); $new_img_url = './images/'.$img_name; file_put_contents($new_img_url, file_get_contents($img_url)); $content = str_replace($img_url, $new_img_url, $content); }

echo $title.'<br />'; echo $content; ?>