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?

Link to comment
Share on other sites

#\[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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.