note 44485 added to function.preg-replace-callback
| From: | Emu-Xperts at osu1 dot php dot net | Date: | Sat, 07 Aug 2004 13:23:50 +0000 |
| Subject: | note 44485 added to function.preg-replace-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74143@lists.php.net to get a copy of this message | ||
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("&", "&", $matches[1]);
//END
//OverRide the above for "\"
$matches[1] = str_replace("\\", "\", $matches[1]);
//END
return "<div style='width:100%;color:red;font-size:13px'>Code:<hr
/><font face='courier'>" . preg_replace("/<(\/)?(.*?)>/",
"<\\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"
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44485
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44485&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44485&report=yes
Search -- http://master.php.net/manage/user-notes.php