note 31849 deleted from function.preg-replace by sniper
| From: | sniper@php.net | Date: | Sun, 07 Sep 2003 22:00:50 +0000 |
| Subject: | note 31849 deleted from function.preg-replace by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-55681@lists.php.net to get a copy of this message | ||
Note Submitter: fie@myrealbox.com
----
in responce to sylvinus at lynucs dot com
and for anyone that needs a replacement code
here is yet another "improved" version
i'm a newb to regular expressions but it works fine
i belive i got the replacement string from the page that
explains regular expressions on here which i also improved.
// NOTE THAT IN THE BELOW CODE THE MATCHING STRINGS
// ARNT SUPPOSED TO HAVE AN RETURNS (ENTER) BUT
// PHP.NET REQUIRES ME TO KEEP MY LINE LENGTH SHORT
// BE SURE TO REMOVE THEM IF YOU USE THIS CODE
function makelinks($str){
$str = preg_replace(
"/(\<\*)(link)(=)(['\"]?)(http:\/\/)
([^\"']*)(\\4)(.*)(\>)/siU",
"<a href=\"http://\\6\"
target=\"_blank\">\\8</a>",
$str
);
// you would use the above as fallows
// <*link="http://xxx.com"SECKS!>
$str = preg_replace(
"/[^\"\'](?:^|\b)((((http|https|ftp):\/\/)|(www\.))
([\w\.]+)([,:%#&\/?=\w+\.-]+))(?:\b|$)/is",
" <a href=\"\\1\">\\1</a>",
$str
);
$str = preg_replace(
"/[^(mailto\:)](([\w\.]+)(@)([\w\.]+))/i",
" <a href=\"mailto:\\1\">\\1</a>",
$str
);
return $str;
}
the first replacement is for my whole StarML code
i'm trying to develop. much like bbcode but yea
anyways. so the second replacement is yours
which i added [^\"\'] to the beginging to make
sure you're not turning urls that are already links
into links... the third replacement is for email
addresses, on this i added [^(mailto\:)] to the
begining as to make sure you're not linking email
addresses that are already linked.
I AM THE SILENT WIND OF DOOM WOOSH!