我需要替换 curl 获取的页面中的 url 并添加正确的图像和链接链接。我的 php curl 代码是:
<?php
function getPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$result = curl_exec($ch);
curl_close($ch);
if (!preg_match('/src="https?:\/\/"/', $result))
$result = preg_replace('/src="(.*)"/', "src=\"http://support.prophpbb.com/\\1\"", $result);
if (!preg_match('/href="https?:\/\/"/', $result))
$result = preg_replace('/href="(.*)"/', "href=\"http://support.prophpbb.com/\\1\"", $result);
return $result;
}
$result = getPage('http://support.prophpbb.com/');
print_r ($result);
?>
此代码适用于某些链接,但对于正确的链接,它会重复。
从错误的链接,替换为正确的:
<img src="./uploads/support/images/1355955233.png" alt="" title="" />
<img src="http://support.prophpbb.com/./uploads/support/images/1355955233.png" alt="" title="" />
但是正确的链接,被替换为错误的:
<img src="http://support.prophpbb.com/styles/subsilverPlus/theme/images/icon_mini_faq.gif" width="12" height="13" alt="*" />
<img src="http://support.prophpbb.com/http://support.prophpbb.com/styles/subsilverPlus/theme/images/icon_mini_faq.gif" width="12" height="13" alt="*" />
有人可以帮我吗?