note 31849 added to function.preg-replace
| From: | fie at myrealbox dot com | Date: | Tue, 06 May 2003 16:04:25 +0000 |
| Subject: | note 31849 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-47585@lists.php.net to get a copy of this message | ||
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!
--
http://www.php.net/manual/en/function.preg-replace.php
http://master.php.net/manage/user-notes.php?action=edit+31849
http://master.php.net/manage/user-notes.php?action=delete+31849
http://master.php.net/manage/user-notes.php?action=reject+31849