wee493 Posted September 23, 2009 Share Posted September 23, 2009 I need to replace all <a href tags linking to an an image because my images are hosted on a different site. For example. I have two links http://example.com/cool.jpg http://example.com/file.pdf Currently I use (Something similar to this, this is just to give you an idea) $link[0] = 'http://example.com/cool.jpg'; $link[1] = 'http://example.com/file.pdf'; str_replace ('http://example.com','http://remotesite.com', $links); I need it to only replace the link for images linking to a picture. My links are not in a array they are in a chunk of text so I don't know how to separate out the link. Does anyone have any ideas? Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 23, 2009 Share Posted September 23, 2009 $links = preg_replace('~http://example\.com/([^/?].(?:jpg|png|gif|jpgeg))~','http://remotesite.com$1',$links); Just put some more formats in there if you can think of them, I'm a bit sick so I probably can't think of em all Quote Link to comment Share on other sites More sharing options...
wee493 Posted September 23, 2009 Author Share Posted September 23, 2009 $links = preg_replace('~http://example\.com/([^/?].(?:jpg|png|gif|jpgeg))~','http://remotesite.com$1',$links); Just put some more formats in there if you can think of them, I'm a bit sick so I probably can't think of em all Thanks, does it matter that my images are in folders like, http://example.com/uploads/2009/09 ? I currently working on this code, not having any luck Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 23, 2009 Share Posted September 23, 2009 No, it just matches http://example.com/anythingwithoutaslashinit.jpg (or .png or .gif or .jpeg) You've have to give a specific pattern of what to look for in the URL to know if it's an image or not (if there is one) Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 23, 2009 Share Posted September 23, 2009 Are your links inside anchor tags? Would make it easier. But else try something like <?php $str = 'Text http://example.com/uploads/2009/09/image.jpg and a non-image http://example.com/about/about.php and an image http://example.com/uploads/images/test.name.gif'; $str = preg_replace('~http://example\.com(\S+\.(?:jpe?g|png|gif))~i', 'http://remotesite.com$1', $str); echo $str; //Text http://remotesite.com/uploads/2009/09/image.jpg and a non-image http://example.com/about/about.php and an image http://remotesite.com/uploads/images/test.name.gif ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.