PHP 测试杂项

时间:2023-03-09 00:36:54
PHP 测试杂项
// 驼峰转下划线
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);

详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别

实例:

1,http://localhost/aaa/ (打开aaa中的index.php)
结果:
$_SERVER['QUERY_STRING'] = "";
$_SERVER['REQUEST_URI']  = "/aaa/";
$_SERVER['SCRIPT_NAME']  = "/aaa/index.php";
$_SERVER['PHP_SELF']     = "/aaa/index.php";

2,http://localhost/aaa/?p=222 (附带查询)
结果:
$_SERVER['QUERY_STRING'] = "p=222";
$_SERVER['REQUEST_URI']  = "/aaa/?p=222";
$_SERVER['SCRIPT_NAME']  = "/aaa/index.php";
$_SERVER['PHP_SELF']     = "/aaa/index.php";

3,http://localhost/aaa/index.php?p=222&q=333
结果:
$_SERVER['QUERY_STRING'] = "p=222&q=333";
$_SERVER['REQUEST_URI']  = "/aaa/index.php?p=222&q=333";
$_SERVER['SCRIPT_NAME']  = "/aaa/index.php";
$_SERVER['PHP_SELF']     = "/aaa/index.php";

由实例可知:
$_SERVER["QUERY_STRING"]  获取查询 语句,实例中可知,获取的是?后面的值
$_SERVER["REQUEST_URI"]   获取 http://localhost 后面的值,包括/
$_SERVER["SCRIPT_NAME"]   获取当前脚本的路径,如:index.php
$_SERVER["PHP_SELF"]      当前正在执行脚本的文件名