Jump to content

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


damdaman

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

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.