博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 字符串截取.支持中文和其他编码,,检查字符串是否是UTF8编码
阅读量:6227 次
发布时间:2019-06-21

本文共 5750 字,大约阅读时间需要 19 分钟。

  hot3.png

/** +---------------------------------------------------------- * 字符串截取,支持中文和其他编码 +---------------------------------------------------------- * @static * @access public +---------------------------------------------------------- * @param string $str 需要转换的字符串 * @param string $start 开始位置 * @param string $length 截取长度 * @param string $charset 编码格式 * @param string $suffix 截断显示字符 +---------------------------------------------------------- * @return string +---------------------------------------------------------- */function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {    if(function_exists("mb_substr"))        $slice = mb_substr($str, $start, $length, $charset);    elseif(function_exists('iconv_substr')) {        $slice = iconv_substr($str,$start,$length,$charset);        if(false === $slice) {            $slice = '';        }    }else{        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";        preg_match_all($re[$charset], $str, $match);        $slice = join("",array_slice($match[0], $start, $length));    }    return $suffix ? $slice.'...' : $slice;}/** +---------------------------------------------------------- * 检查字符串是否是UTF8编码 +---------------------------------------------------------- * @param string $string 字符串 +---------------------------------------------------------- * @return Boolean +---------------------------------------------------------- */function is_utf8($string) {    return preg_match('%^(?:         [\x09\x0A\x0D\x20-\x7E]            # ASCII       | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte       |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs       | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte       |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates       |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3       | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15       |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16    )*$%xs', $string);}function remove_xss($val) {   // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed   // this prevents some character re-spacing such as 
// note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs $val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val); // straight replacements, the user should never need these since they're normal characters // this prevents like
$search = 'abcdefghijklmnopqrstuvwxyz'; $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $search .= '1234567890!@#$%^&*()'; $search .= '~`";:?+/={}[]-_|\'\\'; for ($i = 0; $i < strlen($search); $i++) { // ;? matches the ;, which is optional // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars // @ @ search for the hex values $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ; // @ @ 0{0,7} matches '0' zero to seven times $val = preg_replace('/({0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ; } // now the only remaining whitespace attacks are \t, \n, and \r $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base'); $ra2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload'); $ra = array_merge($ra1, $ra2); $found = true; // keep replacing as long as the previous round replaced something while ($found == true) { $val_before = $val; for ($i = 0; $i < sizeof($ra); $i++) { $pattern = '/'; for ($j = 0; $j < strlen($ra[$i]); $j++) { if ($j > 0) { $pattern .= '('; $pattern .= '(&#[xX]0{0,8}([9ab]);)'; $pattern .= '|'; $pattern .= '|({0,8}([9|10|13]);)'; $pattern .= ')*'; } $pattern .= $ra[$i][$j]; } $pattern .= '/i'; $replacement = substr($ra[$i], 0, 2).'
'.substr($ra[$i], 2); // add in <> to nerf the tag $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags if ($val_before == $val) { // no replacements were made, so exit the loop $found = false; } } } return $val;}

转载于:https://my.oschina.net/yoke/blog/75286

你可能感兴趣的文章
31个Oracle常用问题及命令
查看>>
输入输出 字符串相关
查看>>
request获取url链接和参数
查看>>
腾讯视频播放器V 1.0 去广告补丁
查看>>
实现本地上传的Kindetitor的Servlet版本
查看>>
Android学习笔记—第九章 Activity的加载模式
查看>>
C#设计模式系列:代理模式(Proxy)
查看>>
javaEE项目建立多个数据源并配置事务
查看>>
python-字符串格式化
查看>>
DNS配置笔记
查看>>
Chrome自定义最小字号
查看>>
Android多人视频聊天应用的开发(一)快速集成
查看>>
谷歌web站点安全扫描软件skipfish安装、配置、使用
查看>>
Centos 中如何快速定制二进制的内核 RPM 包
查看>>
zabbix 自动发现tomcat的war包并实现监控
查看>>
网络安全简介
查看>>
Nginx基本概念和安装
查看>>
我的友情链接
查看>>
10年以后第一次做作业的感受
查看>>
SpannableString与SpannableStringBuilder使用
查看>>