I need a way to take a block of HTML code and make all URLs absolute. I've tried to adopt various regex examples out there but had no luck. These are the requirements:
- Replace both HREF and SRC urls
- If URL is already absolute, leave it
- If URL is absolute, replace it
Each HTML comes from a known URL (example.com/folder/file.html) which can be used to create the absolute URLs. For example:
src="image.png" becomes src="http://example.com/folder/image.png" href="/home.html" becomes href="http://example.com/home.html"
I have found a function which does exactly what I need:
http://nashruddin.com/PHP_Script_for_Converting_Relative_to_Absolute_URL
But I can't figure out how to do it in bulk, for all URLs in a block of code.
Any help would be great!
Cheers.