Re: For the RegExps gurus!

From: Date: Wed, 05 Sep 2001 10:15:34 +0000
Subject: Re: For the RegExps gurus!
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-66023@lists.php.net to get a copy of this message
<Original message> From: _lallous <elias_bachaalany@yahoo.com> Date: Wed, Sep 05, 2001 at 12:17:21PM +0200 Message-ID: <20010905093745.77579.qmail@pb1.pair.com> Subject: [PHP] For the RegExps gurus! > I want to write a RegExp that converts something like this: > $mem = " > Hello world! http://www.helloworld.com\n > Hello world! ftp://ftp.server.com\n > Hello world www.helloworld.com" > > how can I write a single regexp that converts $mem to this: > $mem = " > Hello world! <a > > href="http://www.helloworld.com">http://www.helloworld.com</a>\n > Hello world! <a > href="ftp://ftp.server.com">ftp://ftp.server.com</a>\n > Hello world <a > > href="http://www.helloworld.com">http://www.helloworld.com</a>" > > As you noticed that the second line didn't have the protocol specifier and > it took by default "http://" and the ones that had a > protocol specifier were > kept as is. </Original message> <Reply> This should do the job: --- PHP code --- <PRE> <?php $mem = "Hello world! http://www.helloworld.com\n Hello world! ftp://ftp.server.com\n Hello world www.helloworld.com"; /* ------ ! Here's your regexp ! ------ */ $mem = preg_replace ("/\s+([http:|ftp:]{0,1}\S+\.\S+)/me", "' <A href=\"'. (ereg('^[http:|ftp:]','\\1') ? '\\1\">\\1</A>' : 'http://\\1\">http://\\1</A>')", $mem); print ($mem); ?> </PRE> --- End of PHP code --- Let me know if this was what you were looking for... </Reply> -- * R&zE: -- »»»»»»»»»»»»»»»»»»»»»»»» -- Renze Munnik -- DataLink BV -- -- E: renze@datalink.nl -- W: +31 23 5326162 -- F: +31 23 5322144 -- M: +31 6 21811143 -- -- Stationsplein 82 -- 2011 LM HAARLEM -- Netherlands -- -- http://www.datalink.nl -- ««««««««««««««««««««««««

« previous php.general (#66023) next »