Question

I'm capturing data from an external url using php and throwing it back to my page via ajax to load images. My problem is the external url have some images that are relative("images/example.jpg") so I have to prepend their base domain("http://www.example.com) so I can load it in my page. Problem is some of the images includes a base domain url yet some are in their relative form. Im guessing maybe use regex in my javascript to the determines the string if it have a base("http://example.domain.com") or just a relative("images/") to it. How can I achieve that through regex?

Was it helpful?

Solution

If you can parse it in PHP - I'd do what alanhaggai suggested: parse_url. If you need to go this in javascript: just check if there's a protocol at the start (eg: http:// https:// ftp://). Basically check for :// in the first dozen or so characters of the url.

OTHER TIPS

Parse the URL from within the PHP script using parse_url. If the associative array does not contain the keys host or scheme, you can rewrite the URL to include them.

You won't be able to distinguish between a relative url like "images/blank.gif" and a relative url that has "www.theirdomain.com/images/blank.gif". Who is to say that "www.theirdomain.com" isn't a directory?

If the url does not start with http://, https:// or // it is relative to the url of the page where you scraped them from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top