note 92548 added to function.preg-replace-callback
| From: | ixiter at gmail dot com | Date: | Wed, 29 Jul 2009 02:06:54 +0000 |
| Subject: | note 92548 added to function.preg-replace-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158851@lists.php.net to get a copy of this message | ||
When you use preg_replace_callback in a class and have the callback function as a private method of
that class, you need to set the callback function name like className::CallBack.
self::CallBack does not work and returns an error:
"Cannot call method self::CallBack() or method does not exist"!
<?php
class myClass{
public function parsetext($text){
// parses text and sets literals A - C to lower case
// this works
return preg_replace_callback('|([a-c])|i', 'myClass::preg_tolower',
$text);
}
public function parsefail($text){
// parses text and sets literals A - C to lower case
// this fails
return preg_replace_callback('|([a-c])|i', 'self::preg_tolower', $text);
}
private static function preg_tolower($matches){
return strtolower($matches[1]);
}
}
$parser = new myClass;
echo $parser->parsetext('ABCDEFGH');
// echoes abcDEFGH
echo $parser->parsefail('ABCDEFGH');
// throws the error
----
Server IP: 217.13.201.10
Probable Submitter: 78.34.207.15
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php
Edit -- https://master.php.net/note/edit/92548
Del: integrated -- https://master.php.net/note/delete/92548/integrated
Del: useless -- https://master.php.net/note/delete/92548/useless
Del: bad code -- https://master.php.net/note/delete/92548/bad+code
Del: spam -- https://master.php.net/note/delete/92548/spam
Del: non-english -- https://master.php.net/note/delete/92548/non-english
Del: in docs -- https://master.php.net/note/delete/92548/in+docs
Del: other reasons-- https://master.php.net/note/delete/92548
Reject -- https://master.php.net/note/reject/92548
Search -- https://master.php.net/manage/user-notes.php