Home  >  Article  >  Backend Development  >  PHP converts uppercase names into underscores to separate names, php underscore_PHP tutorial

PHP converts uppercase names into underscores to separate names, php underscore_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:56:00965browse

php converts uppercase names into underscores to separate names, php underscores

Sometimes it is necessary to convert uppercase letters in a string to _ lowercase, which will happen when naming variables If you encounter this kind of problem, just go to the code:

$name = 'AppPromoZhongQiu2014ActiveStatusSelector';

echo cc_format($name);
function cc_format($name){
  $temp_array = array();
  for($i=0;$i<strlen($name);$i++){
    $ascii_code = ord($name[$i]);
    if($ascii_code >= 65 && $ascii_code <= 90){
      if($i == 0){
         $temp_array[] = chr($ascii_code + 32);
      }else{
        $temp_array[] = '_'.chr($ascii_code + 32);
      }
    }else{
      $temp_array[] = $name[$i];
    }
  }
  return implode('',$temp_array);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/990552.htmlTechArticlephp converts uppercase names into underscores to separate names. php underscores sometimes need to convert uppercase letters in a string into _ Lowercase, you will encounter this problem when naming variables...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn