Forum Moderators: coopster

Message Too Old, No Replies

Add the <a> tags to text if they don't exist (preg replace)

         

flapane

10:52 am on Oct 24, 2010 (gmt 0)

10+ Year Member



Hi,
I'm trying to add the <a> tag to
http://foo.com
part of the text, trying to avoid messing with the <a> tags if they ALREADY exist somewhere.

Example:
SOURCE:
this is a http://foo.com text <a href="http://bar.com">bar avoid it</a>

DESIDERED RESULT:
this is a <a href="http://foo.com">http://foo.com</a> text <a href="http://bar.com">bar avoid it</a>


I came up with this code:

$item_link = preg_replace("/(?!(?:[^<]+>|[^>]+<\/a>))\b(http)\b/is", "<a href=\"\\1\">\\1</a>",$item_link); 


However I need a wildcard for http, so that it does the substitution until the end of the http address (ie. until it finds a blank space, right?).

I'm clearly a newbie, so I'm sure that the solution might not be hard to find.
Thanks in advance :)

flapane

11:12 am on Oct 24, 2010 (gmt 0)

10+ Year Member



I think I've made it:

((http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*))


It seems to work, but any advice is appreciated.