preg_replace to modify SRC and HREF urls coming from curl

僤鯓⒐⒋嵵緔 提交于 2019-12-11 03:39:18

问题


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="*" />

Can anybody help me please?


回答1:


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


来源:https://stackoverflow.com/questions/19187025/preg-replace-to-modify-src-and-href-urls-coming-from-curl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!