PHP: Why do you need a screened zero after a break?



  • Yii2 is a method https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseInflector.php#L360 :

    public static function camel2words($name, $ucwords = true)
    {
        $label = trim(strtolower(str_replace([
            '-',
            '_',
            '.',
        ], ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));
        return $ucwords ? ucwords($label) : $label;
    }
    

    Why is there a replacement? пробел + экранированный нольWhy not just пробел?



  • Emayo, that's what I' working on...

    An enclosed zero in this case is a reference to the submarine:

    replacement may contain references to the \n or (from PHP 4.0.4) $n, the latter being preferable. Each reference will be replaced by a sub-line corresponding to a n-base. n may be taken from 0 to 99, with reference \0 (or $0) corresponding to the entire cap. The masks are numbered on the left to the right, starting with the unit. To use the reverse surveillance, it needs to be blown up.

    http://php.net/manual/ru/function.preg-replace.php

    UPDATE

    When quoting PHP.Net quotes, it was not quite accurately published: &quot; reversed &quot; . That was noted in the comments to the answer. The quote is accurate. However, as I pointed out, both options are working (i mean, that is, \0 and \\0😞

    $str = 'AaaBbbCcc';
    

    echo preg_replace('/(?<![A-Z])[A-Z]/', ' ', $str); // aa bb cc

    echo "\n";

    echo preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $str); // Aaa Bbb Ccc

    echo "\n";

    echo preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $str); // Aaa Bbb Ccc

    https://repl.it/Cjmh

    Why, I don't know, I think it's worth a separate question.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2