note 44863 added to function.preg-replace-callback
| From: | php-general at lists dot php dot net | Date: | Wed, 18 Aug 2004 14:00:12 +0000 |
| Subject: | note 44863 added to function.preg-replace-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74881@lists.php.net to get a copy of this message | ||
if you want to send some extra parameters to your call back function here's how to do it (using
preg_replace instead!). i took the example from the manual, on how to convert html tags to upper
case...
if you don't use a class:
<?php
$extra_info = "some extra info";
echo preg_replace("/(<\/?)(\w+)([^>]*>)/e", "html_body('\\1',
'\\2', '\\3', '$extra_info')", $html_body);
function html_body($open_tag, $the_tag, $close_tag, $extra_info) {
return $before . strtoupper($middle) . $after;
}
?>
class example:
<?php
class SomeClass {
function SomeClass() {
$extra_info = "some extra info";
echo preg_replace("/(<\/?)(\w+)([^>]*>)/e",
"\\$this->html_body('\\1', '\\2', '\\3',
'$extra_info')", $html_body);
}
function html_body($open_tag, $the_tag, $close_tag, $extra_info) {
return $before . strtoupper($middle) . $after;
}
}
?>
now, if you'd like to pass an array :)
<?php
$extra_info = ("some extra info", "little more", "a bit more");
echo preg_replace("/(<\/?)(\w+)([^>]*>)/e", "html_body('\\1',
'\\2', '\\3', \\$extra_info)", $html_body);
function html_body($open_tag, $the_tag, $close_tag, $extra_info) {
return $before . strtoupper($middle) . $after;
}
?>
you can also pass the array as a reference...cool!
hope this helps someone! ;)
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44863
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44863&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44863&report=yes
Search -- http://master.php.net/manage/user-notes.php