preg_replace修改来自curl的SRC和HREF url [英] preg_replace to modify SRC and HREF urls coming from curl

查看:88
本文介绍了preg_replace修改来自curl的SRC和HREF url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要替换curl所获取页面中的网址,并向图像和链接添加正确的链接。我的php curl代码是:

I need to replace urls in the page taken by curl and add correct link to images and links. My php curl code is:

<?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);

?>

此代码对某些链接有效,但对于正确的链接,它会重复。

This code working ok for some links, but for correct links it make duplicate.

来自错误的链接,被正确的替换:

From wrong link, Is replaced with correct:

<img src="./uploads/support/images/1355955233.png" alt="" title="" />
<img src="http://support.prophpbb.com/./uploads/support/images/1355955233.png" alt="" title="" />

但是正确的链接,被错误的替换:

But correct links, Is replaced with wrong:

<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中尝试此正则表达式

Try this regular expression in 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);

这篇关于preg_replace修改来自curl的SRC和HREF url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆