6

I am requesting the source code of a website like this:

<? $txt = file_get_contents('http://stats.pingdom.com/qmwwuwoz2b71/522741');
echo $txt; ?>

Bu I would like to replace the relative links with absolute ones! Basically,

<img src="/images/legend_15s.png"/> and <img src='/images/legend_15s.png'/>

should be replaced by

<img src="http://domain.com/images/legend_15s.png"/> 

and

<img src='http://domain.com/images/legend_15s.png'/>

respectively. How can I do this?

3 Answers 3

10

This can be acheived with the following:

<?php 
$input = file_get_contents('http://stats.pingdom.com/qmwwuwoz2b71/522741');

$domain = 'http://stats.pingdom.com/';
$rep['/href="(?!https?:\/\/)(?!data:)(?!#)/'] = 'href="'.$domain;
$rep['/src="(?!https?:\/\/)(?!data:)(?!#)/'] = 'src="'.$domain;
$rep['/@import[\n+\s+]"\//'] = '@import "'.$domain;
$rep['/@import[\n+\s+]"\./'] = '@import "'.$domain;
$output = preg_replace(
    array_keys($rep),
    array_values($rep),
    $input
);

echo $output;
?>

Which will output links as follows:

/something

will become,

http://stats.pingdom.com//something

And

../something

will become,

http://stats.pingdom.com/../something

But it will not edit "data:image/png;" or anchor tags.

I'm pretty sure the regular expressions can be improved though.

1
  • I love this! Thank you for writing it. Clever to put the preg_replace parameters into the keys. I implemented this to fulfill a user feature request to set absolute links in my plugin that generates static copies of Grav websites. That said, if anyone finds an issue with it, I will try to report back here about it so future users will have a better copy.
    – user4513271
    Commented Jun 18, 2017 at 6:50
9

This code replaces only the links and images:

<? $txt = file_get_contents('http://stats.pingdom.com/qmwwuwoz2b71/522741');
$txt = str_replace(array('href="', 'src="'), array('href="http://stats.pingdom.com/', 'src="http://stats.pingdom.com/'), $txt);
echo $txt; ?>

I have tested and its working :)

UPDATED

Here is done with regular expression and working better:

<? $txt = file_get_contents('http://stats.pingdom.com/qmwwuwoz2b71/522741');
$domain = "http://stats.pingdom.com";
$txt = preg_replace("/(href|src)\=\"([^(http)])(\/)?/", "$1=\"$domain$2", $txt);
echo $txt; ?>

Done :D

0

You dont need php, you only need to use the html5 base tag, and put your php code in html body, you only need to do the following Example :

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <base href="http://yourdomain.com/">
</head>
<body>
<? $txt = file_get_contents('http://stats.pingdom.com/qmwwuwoz2b71/522741');
echo $txt; ?>
</body>
</html>

and all the files will use the absolute url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.