Home >Backend Development >PHP Tutorial >php把大写命名转换成下划线分割命名_PHP

php把大写命名转换成下划线分割命名_PHP

WBOY
WBOYOriginal
2016-05-30 15:09:201242browse

有时候需要把一个字符串中的大写转换成 _+小写的方式,在变量命名的时候会碰到这种问题,直接上代码:

$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);
}

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