I tried looking for a similar question, but was unable to.
I'm looking for push in the right direction. What I'm currently doing is gathering a list of all href values of a remote site, now since some of them can be relative paths, I need a function that builds an absolute path.
Since I have the domain name (by following the last url cUrl used):
$base_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
Now lets say $base_url value is: http://www.example.com/home/index.html and href value I'm currently reading is: /styles/ie.css
I need to turn the value of $base_url to http://www.example.com/styles/ie.css but I need that function to be dynamic as much as possible. Take a look at the possible scenarios (probably a not all):
1 = base url
2 = relative path
------------------------------------------------
1 http://www.example.com/
2 java/popups.js
1 + 2 = http://www.example.com/java/popups.js
------------------------------------------------
1 http://www.example.com
2 java/popups.js
1 + / + 2 = http://www.example.com/java/popups.js
------------------------------------------------
1 http://www.example.com/mysite/
2 ../java/popups.js
1 - / + (2 - ..) = http://www.example.com/java/popups.js
------------------------------------------------
1 http://www.example.com/rsc/css/intlhplib-min.css
2 ../images/sunflower.png
1 - /css/intlhplib-min.css + (2 - ..) = http://www.example.com/rsc/images/sunflower.png
I think you would need to use regular expressions on the href path to make sure that it is consistent. You can also get an accurate base url from parse_url():
Here we strip the periods and the slashes from the beginning of the string. And then prepend the base url:
Should output what you want:
http://www.example.com/images/sunflower.png
UPDATE
If you want the first directory from the base url, then use explode on the parsed url's path key:
And add that to the output variable:
Another Update
To find how the href values relate to the base url, you can search for the occurence of
../
or/
at the beginning of the href value, and then adjust your absolute url accordingly. This should help you figure out what the scenarios are:Check the demo on IDEONE.
I ended up writing my own function, after a push in the right direction from @bozdoz.
The function takes two arguments, first one is $resource, which is the relative file path. And the second one is is the base url (which will be used to construct an absolute url).
This was design for my project purposes, I'm not sure it will fit anyone who is looking for a similar solution. Feel free to use it, and provide any efficiency improvements.
Updated version Thanks to Tim Cooper