note 18791 added to function.preg-replace-callback
| From: | phpdoc at kreapc dot de | Date: | Tue, 05 Feb 2002 22:03:55 +0000 |
| Subject: | note 18791 added to function.preg-replace-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-26273@lists.php.net to get a copy of this message | ||
Here's some nice example for preg_replace_callback() for threaded forums.
Replies with '>' quotes are autmatically colored according to the reply level, and URL
and mail adresses are highlighted.
function quote_callback($s)
{
$colors = array("red", "green", "blue", "purple",
"black" );
$level = substr_count($s[1],">");
$color = $colors[min($level, count($colors))-1];
return "<font color=$color><i>$s[0]</i></font>";
}
function url_callback($s)
{
if ($s[1])
return "<a href=\"$s[1]\" target=_blank>$s[0]</a>";
if ($s[5])
return "<a href=\"mailto:$s[5]\">$s[0]</a>";
return $s[0];
}
function highlight($text)
{
$text = htmlspecialchars($text);
$text = str_replace("\r", "", $text);
// highlight quoted lines
//
$text = preg_replace_callback(
'/^((> *)+).*$/m',
'quote_callback', $text
);
// replace URL's and Mail adresses
//
// (these must be done together, so that a
// http://user@domain.tld is interpreted as
// an URL only.)
//
$text = preg_replace_callback(
'/((https?|ftp|telnet):\/\/([\w.:?#@_=%\/+-]|(&))+)|'.
'([\w_-]+(\.[\w_-]+)*@[\w_-]+(\.[\w_-]+)+)/',
'url_callback', $text
);
return $text;
}
--
http://www.php.net/manual/en/function.preg-replace-callback.php
http://master.php.net/manage/user-notes.php?action=edit+18791
http://master.php.net/manage/user-notes.php?action=delete+18791
http://master.php.net/manage/user-notes.php?action=reject+18791