🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[toc] ## :-: **去掉下划线 && 首字母转成大写** ```php /** * 字符串去掉下划线&&首字符转成大写 * @param $str 待转换的字符串 * @return string */ function myucfirst($str){ $arr = explode('_',$str); //去掉_ $temp = ''; //如果下划线和大于1个就循环去掉下划线 && 首字母转换为大写 if(count($arr) > 1){ foreach($arr as $key => $value){ $temp .= ucfirst($value); } }else{ //否则 纯字符 首字母转大写 $temp = ucfirst($str); } return $temp; } ``` ## :-: **正则 去掉 html标签** ```php /** * 返回指定长度的文本(正则匹配去掉HTML标识) * @param $html * @param $max * @return string */ function getClearText( $html , $max = 0 ) { if ( empty( $html ) ) { return ''; } $html = preg_replace( '/<[^>]*>/' , '' , $html ); $html = preg_replace( '/[\r\n\t]/' , '' , $html ); if ( $max > 0 ) { $len = strlen( $html ); if ( $len > $max ) { $tmp0 = 0; $tmp1 = 0; $tmp2 = 0; preg_match_all( '/./us' , $html , $match ); foreach ( $match[0] as $value ) { $tmp0 = strlen( $value ); if ( $tmp0 == 1 ) { $tmp1 += 1; } else { $tmp1 += 2; } $tmp2 += $tmp0; if ( $tmp1 >= $max ) { $html = substr( $html , 0 , $tmp2 ); break; } } } } return $html; } ``` ## :-: **防xss攻击** ```php /** * 过滤字符串,防止xss攻击 * @param $string * @param bool $low 安全级别低 * @return bool */ function clean_xss(&$string, $low = false) { if (! is_array ( $string )) { $string = trim ( $string ); $string = strip_tags ( $string ); $string = htmlspecialchars ( $string ); if ($low) { return True; } $string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string ); $no = '/%0[0-8bcef]/'; $string = preg_replace ( $no, '', $string ); $no = '/%1[0-9a-f]/'; $string = preg_replace ( $no, '', $string ); $no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; $string = preg_replace ( $no, '', $string ); return True; } $keys = array_keys ( $string ); foreach ( $keys as $key ) { clean_xss ( $string [$key] ); } } die; //测试xss攻击 echo $str = '<meta http-equiv="refresh" content="0;">'; //网页不停地刷新 echo $str = "<iframe src=http://xxxx width=250 height=250></iframe>"; //嵌入其它网站的链接 echo $info = clean_xss($str); //调用 ``` ## :-: **字符串 转 数组** ```php /** * 字符串 转变为 数组格式 * @param $query 可以使用parse_url()获取到的query参数。 * @return array array (size=10) */ function strToArr($query) { $queryParts = explode('&', $query); $params = array(); foreach ($queryParts as $v) { $item = explode('=', $v); $params[$item[0]] = $item[1]; } return $params; } ``` ## :-: **数组 转 字符串** ```php /** * 数组 转变为 字符串格式 * @param $array_query * @return string string 'm=content&c=index' */ function arrToStr($array_query) { $tmp = array(); foreach($array_query as $k=>$v) { $tmp[] = $k.'='.$v; } $params = implode('&',$tmp); //拿&分割 return $params; } ``` ## :-: **url分层** ```php /** * 以/分割,获取url的层级 * @param $url 待操作的url * @return array * @author yzm */ function getUrlDevel($url){ $arr = parse_url($url); $strs = explode('/',$arr['path']); $temp = []; $temp[] = $arr['scheme']; $m = $arr['scheme']."://".$arr['host']; $temp[] = $arr['scheme']."://".$arr['host']; $t = ''; for ($i=1;$i<count($strs);$i++) { $t .= '/'.$strs[$i]; $temp[] .= $m.$t; } return $temp; } ``` 调用示例: $url = 'http://172.16.10.81/web/index.php/test '; ![](https://box.kancloud.cn/7367e7b401b54f9b87c7a2107186a2fb_517x120.png) ## :-: **不连续的url分层** ```php /** * 以/分割,返回不连续url的层级 * @param $url 待操作的url * @return array */ function getUrlDevel($url){ $arr = parse_url($url); $temp = []; $temp[] = $arr['scheme']; //将http塞入临时数组 $temp[] = $arr['scheme']."://".$arr['host']; //将http://172.16.10.81塞入临时数组 $strs = explode('/',$arr['path']); //去除http://172.16.10.81 剩余的 foreach($strs as $k => $v){ $temp[] = $v; } //搜索$v中的空值,返回的是键名 $del = array_search('', $temp); if ($del !== false){ //要删除的 键名 存在 array_splice($temp, $del, 1); //只删除一个键 } return $temp; } 不连续url的层级 * @param $url 待操作的url * @return array */ function getUrlDevel($url){ $arr = parse_url($url); $temp = []; $temp[] = $arr['scheme']; //将http塞入临时数组 $temp[] = $arr['scheme']."://".$arr['host']; //将http://172.16.10.81塞入临时数组 $strs = explode('/',$arr['path']); //去除http://172.16.10.81 剩余的 foreach($strs as $k => $v){ $temp[] = $v; } //搜索$v中的空值,返回的是键名 $del = array_search('', $temp); if ($del !== false){ //要删除的 键名 存在 array_splice($temp, $del, 1); //只删除一个键 } return $temp; } ``` 调用示例: $url\='http://172.16.10.81/web/index.php/test '; ![](https://box.kancloud.cn/66ac7924f1f8dc604e9865f0f4ba4eb3_367x131.png)