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

php把大写命名转换成下划线分割命名_php技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 20:16:46
Original
1096 people have browsed it

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

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

Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template