Home  >  Article  >  Backend Development  >  How to convert uppercase names to underscore separated names in php

How to convert uppercase names to underscore separated names in php

墨辰丷
墨辰丷Original
2018-06-11 14:10:402066browse

This article mainly introduces how PHP converts uppercase naming into underscore-separated naming. This article explains how to convert some naming methods that are not accustomed to the uppercase style, such as Pascal naming and camel case naming. Friends who need it can refer to it

Sometimes it is necessary to convert the uppercase characters in a string to _ lowercase. You will encounter this problem when naming variables. Just enter 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[] = &#39;_&#39;.chr($ascii_code + 32);
      }
    }else{
      $temp_array[] = $name[$i];
    }
  }
  return implode(&#39;&#39;,$temp_array);
}

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP implements the function of dynamically adding image watermarks according to the color environment

The role and usage of type prompts in PHP

PHP implements the function of capturing pictures from online albums with anti-leeching settings

The above is the detailed content of How to convert uppercase names to underscore separated names in php. For more information, please follow other related articles on the PHP Chinese website!

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