note 44485 deleted from function.preg-replace-callback by aidan

From: Date: Sat, 07 Aug 2004 15:30:10 +0000
Subject: note 44485 deleted from function.preg-replace-callback by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-74144@lists.php.net to get a copy of this message
Note Submitter: Emu-Xperts ---- For BB-Code preg_replace can be very usefull... Here is an example of a "code" tag that will not parse any HTML that is inside, because you want to show the source, not the output. <? //Assuming the variable is $msg //Of course, this can be set dynamically, but for an example it will be static. $msg="Some code i made: [code] <html> <head> <title>This is a title</title> </head> <body background="000000" font="FFFFFF"> <p>Some text... yadayadayada...</p> </body> </html> [/code] "; $msg = preg_replace_callback("/\[code\](.*?)\[\/code\]/si", "html_no_parse", $msg); function html_no_parse($matches) { //Do not parse DECIMAL counterparts $matches[1] = str_replace("&", "&#38;", $matches[1]); //END //OverRide the above for "\" $matches[1] = str_replace("\\", "&#92;", $matches[1]); //END return "<div style='width:100%;color:red;font-size:13px'>Code:<hr /><font face='courier'>" . preg_replace("/<(\/)?(.*?)>/", "&lt;\\1\\2>", $matches[1]) . "</font><hr /></div>"; } ?> As you can see this calls a function that uses preg_replace to do further editing within what is between the "tags"

« previous php.notes (#74144) next »