Jump to content

Convert absolute/realative URLs to Exact URL


The Little Guy

Recommended Posts

I would like to convert all links to "Exact" links, so for example, I want

 

/dir/page.html -> http://somesite.com/dir/page.html

 

dir/page.html -> http://somesite.com/dir/page.html

 

http://somesite.com/dir/page/html -> http://somesite.com/dir/page/html

 

$file_contents = preg_replace('~<a(.+?)href="(.+?)"~','<a$1href="http://dudeel.com/lProxy?p='.str_replace('//','/',$url.'/$2"'),$file_contents);

 

I want the new URL to look like this:

 

http://dudeel.com/lProxy?p=http://somesite.com/dir/page.html

 

does all that make sense?

See if this works for your data set:

 

<pre>
<?php
$data = <<<DATA
<a href="/dir/page.html">A</a> -> http://somesite.com/dir/page.html
<a href="dir/page.html">B</a> -> http://somesite.com/dir/page.html
<a href="http://somesite.com/dir/page/html">C</a> -> http://somesite.com/dir/page/html
DATA;

echo htmlspecialchars(
	preg_replace(
		'%(<a[^>]+)href="(?:http://[^/]+/|/)?([^"]+)"%',
		'$1href="http://dudeel.com/lProxy?p=http://somesite.com/$2',
		$data
	)
);
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.