Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make relative links into absolute ones

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?

like image 271
Chriswede Avatar asked Mar 17 '12 17:03

Chriswede


2 Answers

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.

like image 90
pachanka Avatar answered Oct 07 '22 07:10

pachanka


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

like image 27
ajimix Avatar answered Oct 07 '22 08:10

ajimix