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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.