[英]preg_replace to modify SRC and HREF urls coming from curl
我需要替换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="*" />
有人可以帮我吗?
在preg_replace中尝试此正则表达式
<?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="(http:\/\/([^\/]+)\/)?([^"]+)"/', "src=\"http://support.prophpbb.com/\\3\"", $result);
}
if (!preg_match('/href="https?:\/\/"/', $result)) {
$result = preg_replace('/href="(http:\/\/([^\/]+)\/)?([^"]+)"/', "href=\"http://support.prophpbb.com/\\3\"", $result);
}
return $result;
}
$result = getPage('http://support.prophpbb.com/');
print_r ($result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.