Remote images in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ILoveJackDaniels
Forum Commoner
Posts: 43
Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK

Remote images in PHP

Post 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! :)
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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">
User avatar
ILoveJackDaniels
Forum Commoner
Posts: 43
Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK

Post 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... :)
Post Reply