Make relative links into absolute ones
Asked Answered
C

3

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?

Corse answered 17/3, 2012 at 17:36 Comment(0)
O
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

Ossified answered 17/3, 2012 at 17:43 Comment(0)
D
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.

Diphthongize answered 17/10, 2012 at 18:55 Comment(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.Kissable
O
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

Ossified answered 17/3, 2012 at 17:43 Comment(0)
O
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

Oink answered 21/7, 2017 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.