// 驼峰转下划线function humpToUnderline($str){ ???if(empty($str)){ ???????return ""; ???} ???$arr = str_split($str); ???$temp = ""; ???foreach($arr as $key => $val){ ???????$ascii = ord($val); ???????if( $ascii >= 65 && $ascii <= 90 ){ ???????????if($key == 0){ ???????????????$temp .= chr($ascii+(97-65)); ???????????}else{ ???????????????$temp .= "_".chr($ascii+(97-65)); ???????????} ???????}else if( $ascii >= 97 && $ascii <= 122 ){ ???????????$temp .= chr($ascii); ???????}else{ ???????????$temp .= ""; ???????} ???} ???return $temp;}// 下划线转驼峰function underlineToHump($str){ ???if(empty($str)){ ???????return ""; ???} ???$arr = explode("_", $str); ???foreach($arr as $key => $val){ ???????$arr[$key] = ucfirst( strtolower($val) ); ???} ???return implode("",$arr); ???// return implode("",array_map(‘ucfirst‘,array_map(‘strtolower‘,explode("_", $str))));}// ASCII A - 65// ASCII Z - 90// ASCII a - 97// ASCII z - 122$str = "ABC";echo humpToUnderline($str);echo "<br>";$str = "a_b_____c";echo underlineToHump($str);exit;preg_match_all("/([A-Z]{1}).*[A-Z]*/", $str, $matches);// foreach($matches[1] as $key => $val){// ????echo strpos($str,$val)."/";// }var_dump($matches);// $str = "AaBbCc";// preg_match_all("/([A-Z]{1})(.)/", $str, $matches);// $matches[0] 匹配全体字符串// $matches[1] 匹配第一个括号里面字符串// $matches[2] 匹配第二个括号里面字符串if(count($matches[1]) > 0){ ???$res = array_map(function($v){ ???????return "_".strtolower($v); ???}, $matches[0]);}$res = ltrim( implode("", $res),"_" );var_dump($res);// $str = preg_replace(‘/([A-Z]{1})/‘,"strtoupper(‘$1‘)",$str);
PHP 测试杂项
原文地址:https://www.cnblogs.com/jiangxiaobo/p/9290840.html