Page 1 of 1

Remote images in PHP

Posted: Tue Apr 01, 2003 5:11 am
by ILoveJackDaniels
I've written a small application to perform spell checking on a page, then display that page with misspelled words highlighted. The page that is checked is consequently displayed on a different server, so any page with relative image paths is not going to display correctly, and ideally I'd like it to, well, not do that.

What I need to do is the following, but I'm stuck and don't know how...

1) Grab the location of the page being checked. I.E. if http://www.somesite.com/dir1/dir2/page.htm was being checked, I'd need to have a variable with value http://www.somesite.com/dir1/dir2/ from that (so grabbing the folder the page is in and the url).

2) Construct an eregi_replace or preg_replace command that takes any img tag where the src does not start with http://, and add in the variable from 1.

So an image tag like <img src=http://www.somesite.com/dir1/dir2/image.gif> would remain unchanged, but <img src=image.gif> would be changed to <img src=http://www.somesite.com/dir1/dir2/image.gif>.

Any idea would be more than welcome! :)

Posted: Tue Apr 01, 2003 10:11 am
by daven
check out the $_SERVER[] variables (in the manual under predefined variables). They have the url data you want.

For your replace function, remember that there are three ways people write tags, with or without quotes:
1. <img src=http>
2. <img src='http'>
3. <img src="http">

Posted: Tue Apr 01, 2003 11:03 am
by ILoveJackDaniels
Unfortunately, the $_SERVER variables won't have that data, as I'm grabbing a remote file, not calling one from the server. I need to alter the value of $url, which is the page to be spellchecked, to grab the location of the file being grabbed...

And the images - thankyou, I didn't think of the quotes. However, the real problem is getting together a regex that will match anything that starts with <img, find the appropriate src= attribute, check for an http:// at the start of the image location, and add the location from part 1) in after the src= if there is no http:// there already. If that makes sense... :)