note 20801 deleted from function.preg-replace by sniper
| From: | sniper@php.net | Date: | Sun, 07 Sep 2003 22:08:22 +0000 |
| Subject: | note 20801 deleted from function.preg-replace by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-55713@lists.php.net to get a copy of this message | ||
Note Submitter: andreas.schempp@gmx.ch
----
to replace an url within a text with a link, use this
$string = preg_replace("((http://|ftp://)?
([a-zA-Z1-9\-_\.]+\.[a-zA-Z1-9\-_]+\.[a-z]{2,3}
(/[a-z\-/\._\?=&]+)?))", "<a href=\"\\1\\2\"
target=\"_blank\">\\0</a>", $string);
the first preg_replace will search for url like http://www.php.net,
ftp://something.php.net,
www.php.net/manual/add-note.php?function.preg
this will be replaced with a link
$string = preg_replace("((href=\")
([a-zA-Z1-9\-_\.]+\.[a-zA-Z1-9\-_]+\.[a-z]{2,3}
(/[a-z\-/\._\?=&]+)?))", "href=\"http://\\2", $string);
if you have just www.php.net, it will create a link like href=\"www.php.net\". this will
not work. for all this, we'll search for this (href=\"url) an replace it with
href=\"http://url
hope this will be my last note about this :) (visit ereg_replace...)
as you can see, this feature is better than the one from php.net, because it will include non-http
urls too.
I'll make a feature-request about this, too.