note 20801 added to function.preg-replace
| From: | andreas dot schempp at gmx dot ch | Date: | Tue, 16 Apr 2002 17:17:03 +0000 |
| Subject: | note 20801 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-29446@lists.php.net to get a copy of this message | ||
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.
--
http://www.php.net/manual/en/function.preg-replace.php
http://master.php.net/manage/user-notes.php?action=edit+20801
http://master.php.net/manage/user-notes.php?action=delete+20801
http://master.php.net/manage/user-notes.php?action=reject+20801