note 18791 deleted from function.preg-replace-callback by sniper

From: Date: Tue, 30 Sep 2003 12:27:39 +0000
Subject: note 18791 deleted from function.preg-replace-callback by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57459@lists.php.net to get a copy of this message
Note Submitter: phpdoc@kreapc.de ---- 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],"&gt;"); $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( '/^((&gt; *)+).*$/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.:?#@_=%\/+-]|(&amp;))+)|'. '([\w_-]+(\.[\w_-]+)*@[\w_-]+(\.[\w_-]+)+)/', 'url_callback', $text ); return $text; }

« previous php.notes (#57459) next »