views:

197

answers:

3

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?

A: 

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.

Alan Haggai Alavi
I have editted my description to a clearer one. I can't do the parsing of a url through php because im getting a lot of html tags and passing them back to my page via jquery then load it from there.
cybervaldez
A: 

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.

Mario Menger
if an image has src="www.theirdomain.com/images/blank.gif" then it IS a directory.
nickf
A: 

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.

nickf
thanks! this might be the best thing for me right now.
cybervaldez