Jump to content

regular expressions - preg_replace ignore spaces


susanBuck

Recommended Posts

I need my preg_replace to ignore white space..

 

for example -

I have a string that holds some text and a URL surrounded by some bulletin board img tags

$message = "Hi this is a post in a forum. Check out this image : [img=http://link.com/image.jpg]";

 

this preg_replace takes the url from inbetween the img tags and passes it to a function called shrinkImage

 

$message = preg_replace('#\[img\]((ht|f)tps?://[^www.photojojo.com])([^\s<"]*?)\.(jpg|jpeg|png|gif).*\[/img\]#e', 'shrinkImage(\'$1$3.$4\', true)', $message);

 

I need the above preg_replace to work even if there are spaces between the img tags... ie..

$message = "Hi this is a post in a forum. Check out this image : [img= http://link.com/image.jpg ]";

or

$message = "Hi this is a post in a forum. Check out this image : [img=http://link.com/image.jpg ] ";

or

$message = "Hi this is a post in a forum. Check out this image : [img= http://link.com/image.jpg] ";

 

 

Any help?

#\[img\]\s*((ht|f)tps?://[^www.photojojo.com])([^\s<"]*?)\.(jpg|jpeg|png|gif)\s*\[/img\]#

 

[^www.photojojo.com] is not preventing a string match, but individual character matches. I think you want (?!www\.photojojo\.com) here.

 

(jpg|jpeg|png|gif) can be reduced to (jpe?g|png|gif).

 

Also, use (?: ... ) when you do not need to capture a group.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.