0

これは機能せず、空の文字列を出力します。

$check["pattern"] = "correct";
$text = "Could this be correct?";
echo preg_replace_callback($check["pattern"],ucfirst,$text);

組み込み関数を使用すると良かったでしょう。実際、一般的なコールバックでは、 http://php.net/manual/en/language.types.callable.phpに従って組み込み関数を使用できますが、preg_replace_callbackは使用できません。PHPの機能リクエストである可能性がありますか?

4

2 に答える 2

3

Your code should trigger a notice and a warning:

  • Notice: Use of undefined constant ucfirst - assumed 'ucfirst'
  • Warning: preg_replace_callback(): Delimiter must not be alphanumeric or backslash

If it doesn't, you seriously need to check your PHP error reporting settings. Fixing the code with the help of the error messages:

$check["pattern"] = "/correct/";
$text = "Could this be correct?";
echo preg_replace_callback($check["pattern"],'ucfirst',$text);

... we get this:

Warning: ucfirst() expects parameter 1 to be string, array given

So using a builtin callback function is working fine. However, as the manual page for ucfirst() explains, the function expects a string, not an array. And, as the manual page for preg_replace_callback() explains:

A callback that will be called and passed an array of matched elements in the subject string.

To sum up: it isn't a sensible feature request, it's a bug in your code ;-)

于 2012-09-13T09:58:51.187 に答える
2

It works just fine with all functions. The problem is that those functions expect certain parameters. ucfirst expects strings as input, but preg_replace_callback is passing an array of matches.

So... if you have a built-in function whose signature is compatible with a preg_replace callback signature, it works. But no, not all built-in functions have a compatible signature.

于 2012-09-13T10:00:25.493 に答える