Jump to content

How to convert all links to "absolute links"


physaux

Recommended Posts

Hi guys, I am getting a page's contents using curl, but I am running into a problem.

 

Some of the links that I find, are "relative links", i.e.

<a href="/about/">About</a>

and I already have the urls extracted in an array as such:

links[0]['url']="/about/";

links[0]['anchor']="/about/";

...

 

My question was, is there any fast way to change "/about/" to "http://mydomain.com/about"?

I tried just appending the link to the current domain, but that becomes a problem if I am say on page:

mydomain.com/index.php  ---->  about.php    ==== mydomain.com/index.phpabout.php

How can I get it to be mydomain.com/about.php??

 

I just need some guide lines, all the ways I have tried are not working. Thank you!!

If the relative link starts with "/", you can strip the url down to the domain name and then add the relative link.  If it doesn't, then you can remove any filename component at the end of the current url and THEN append it to whatever is left.

 

 

Well, I think the first poster had the right idea, I want a "operation" to do this effectively. I was just wondering, how could I strip the current url down to the domain? Any ideas how to do that? I'm then going to append with only 1 "/", stripping others if there are more.

As long as your urls are correctly formed, parse_url() will give you all the individual bits.  Then you can paste together the ones you need.

 

I think this function expects them to look like http://domain.com/path/file.html.  I'm not sure if it works if you leave out the "http://", for example.

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.