Svish Svish - 1 year ago 120
HTML Question

PHP: Translate relative img src urls to absolute

I have fetched an HTML page using cURL into a string and loaded it up in a DOMDocument. There I can get all the img tags and their source attributes. My problem now is... how can I make these URLs absolute?

The list of URLs can contain all kinds of variants, for example:

  • foobar.jpg


  • /foobar.jpg

  • ../foobar.jpg

  • folder/foobar.jpg

If the HTML is fetched from an arbitrary URL, what is a safe way of converting these image URLs into absolute ones? Is there a way you can take the base tag into consideration too?

Answer Source

Here is great PHP example how to do this.

function rel2abs($rel, $base) { 
// something

More good examples:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download