简体   繁体   中英

PHP: Relative to absolute URLs in curl'ed html

I am retrieving a simple HTML page using curl in PHP:

<?php
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "http://example.com/server");
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_exec($ch);
 curl_close($ch);
?>

and include ing it in a file on my website:

<?php include('curlingCodeAbove.php');?>

I want to replace the relative URLs in the curl ed HTML with absolute URLs. I am familiar with Change a relative URL to absolute URL but I have tried unsuccessfully to insert that solution into this code.

Fixed it myself. Set the CURLOPT_RETURNTRANSFER flag.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/server");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://mydomain.com/$2$3', $result);
echo $result
?>

While it's probably bad form to accept your own answer, hey, I'm the one who figured it out.

The way I do it is search for ="/ and replace with ="http://www.example.com/

for example:

$replaces['="/'] = '="http://www.example.com/';
$output = str_replace(array_keys($replaces), array_values($replaces), $output);

I have not checked this but it should do what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM