带有var_dump的preg_replace_callback()的异常输出

<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
    //return strtoupper($match[1]);
        var_dump($match);
    }, 'hello-world');
?>

This is a modification on the Example #1 on http://php.net/manual/en/functions.anonymous.php . The var_dump within the anonymous function outputs this:

array(2) { [0]=> string(2) "-w" [1]=> string(1) "w" } helloorld

Anybody has an idea what may be going on?

Thanks.

This should explain the regex part. Now to the echo part where -w is missing: as you can see, preg_replace_callback does operations on $match. Since $match[0] is your matched string, preg_replace_callback expects a replacement as return value in the anonymous function. You have skipped that part in your example, thus, the replacement is empty.

In your code

echo preg_replace_callback('~-([a-z])~', function ($match) {
    //return strtoupper($match[1]);
    var_dump($match);
}, 'hello-world');

You're trying to replace -([a-z]) (-w matches it) with nothing, as your callback returns nothing.

So, replacing -w with nothing (which is casted to empty string) in string hello-world gives you helloorld.