Re: Another regular expression
| From: | Daniel Convissor | Date: | Wed, 29 Nov 2000 04:50:52 +0000 |
| Subject: | Re: Another regular expression | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-27772@lists.php.net to get a copy of this message | ||
Hi Ivan:
rodrigo wrote:
>
> \[link:(.+)\|(.+)\]
>
> this matches the first occurrence of a string like
> "[link:http://www.mlb.com|MLB]" without problems. But
> if there are more
> than one ocurrences it behaves unexpectedly.
While tweaking your regular expression will get the results you want, you'd
be better off with a slghtly different approach. Your method will permit
nasty people to place JavaScript and/or illegal characters into the link.
This poses a security hazzard.
The following regular expression takes any text that's a URI and turns it
into a hyperlink. It works fine with multiple occurances.
eregi_replace("(http://|https://|ftp://|gopher://|news:|mailto:)
([[:alnum:]/!#$%&'()*+,.:;=?@~-]+)([[:alnum:]/!#$%&'()*+:;=?@~-])",
"<a href=\"\\1\\2\\3\">\\1\\2\\3</a>", "$String");
So, for example... before:
blah http://www.foo.org/ moo.
after:
blah <a href="http://www.foo.org/">http://www.foo.org/</a> moo.
Enjoy,
--Dan
--
PHP scripts that make your job easier
http://www.analysisandsolutions.com/code/
SQL Solution | Layout Solution | Form Solution
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
4015 7 Ave, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409