Jump to content

preg_replace, capturing strings that DON'T end in...


Recommended Posts

Hey everyone, new to regexp, still sifting through a pile of books.  I'm using some public code to change plain text urls and images into html links, trying to modify it slightly to fit my needs.

 

The problem is once I convert the images, I want the next preg_replace to convert all the links in the text that DON'T end in .gif, .jpg, or .jpeg.  I've been playing around with the statement for awhile and can't seem to get it to work correctly.

 

Any help would be appreciated. :)  Here is the code I have:

 

  $imgs = "\.gif|\.jpg|\.jpeg";

  $text =
preg_replace('/(?<!S)((http(s?):\/\/)|(www.))+([\w.1-9\&=#?\-~%;\/]+('
. $imgs . '))/',
'<img src="http$3://$4$5" border="0">', $text);
  
  $text =
preg_replace('/(?<!S)((http(s?):\/\/)|(www.))+([\w.1-9\&=#?\-~%;\/]+)/',
'<a href="http$3://$4$5" target="_blank">http$3://$4$5</a>', $text);

 

I know I need to tag something on to the end of this:

 

...([\w.1-9\&=#?\-~%;\/]+)/'

 

...but like I said all my attempts have failed.

 

Thanks in advance if someone can point me in the right direction....

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.