Home  >  Article  >  Backend Development  >  PHP string interception, supports Chinese and other encodings_PHP tutorial

PHP string interception, supports Chinese and other encodings_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:10:56855browse

This article summarizes the function to intercept strings based on the PHP function substr. However, when encountering Chinese, half of the Chinese characters are intercepted and garbled characters appear. The following introduces the interception program that supports Chinese and other encodings.

The code is as follows Copy code
 代码如下 复制代码

/**
* 字符串截取,支持中文和其他编码
*
* @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"))
mb_substr($str, $start, $length, $charset);
elseif(function_exists('iconv_substr')) {
iconv_substr($str,$start,$length,$charset);
}
$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));
if($suffix) return $slice."…";
return $slice;
}

/**

* String interception, supports Chinese and other encodings

*

* @static
* @access public
* @param string $str The string that needs to be converted
* @param string $start starting position
* @param string $length truncation length

* @param string $charset encoding format

* @param string $suffix truncation of display characters
* @return string
*/
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if(function_exists("mb_substr"))

mb_substr($str, $start, $length, $charset);

elseif(function_exists('iconv_substr') ) {

Iconv_substr($str,$start,$length,$charset);

}

$re['utf-8'] = "/[x01-x7f]|[xc2-xdf] [x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
代码如下 复制代码

function msubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
            $tmpstr .= substr($str, $i, 2);
            $i++;
        } else
            $tmpstr .= substr($str, $i, 1);
    }
    return $tmpstr;
}

$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)); if($suffix) return $slice."…" ; Return $slice;}
If we directly use php substr to intercept data, such as in English and When Chinese characters are mixed, the following problems will occur: If there is such a string $str="This is a string"; In order to intercept the first 10 characters of the string, use if(strlen($str)>10) $str=substr($str,10)."…"; Then, the output of echo $str should be "This is a word..."Suppose$str="This is a string"; This string contains a half-width character, the same execution: if(strlen($str)>10) $ str=substr($str,10); Since the 10th and 11th characters of the original string $str constitute the Chinese character "character"; After performing string division, the Chinese character will be divided into two, so The intercepted string will be garbled. Using the above code character interception code can easily solve this problem. Today I found a better way to intercept Chinese strings, and I would like to share it with you.
The code is as follows Copy code
function msubstr($str, $start, $len) { $tmpstr = ""; $strlen = $start + $len; for($ i = 0; $i < $strlen; $i++) {<🎜>                                                                                                                                                        $i, 2);<🎜> $i++;<🎜> } else<🎜> $tmpstr .= substr($str, $i, 1);<🎜> }<🎜> return $tmpstr;<🎜>} <🎜>

Program 2: PHP intercepts UTF-8 strings and solves the half-character problem

/***************************************************** ******************
* PHP intercepts UTF-8 strings to solve the half-character problem.
* English and numbers (half-width) are 1 byte (8 bits), Chinese (full-width) are 3 bytes
* @return The string taken out, when $len is less than or equal to 0, the entire character will be returned String
* @param $str Source string
* $len The length of the left substring
**************************** *****************************************/

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
The code is as follows
代码如下 复制代码
function utf_substr($str,$len)
{
for($i=0;$i<$len;$i++)
{
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127)
{
$i++;
if($i<$len)
{
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
}
else
{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
return join($new_str);
}
?>
Copy code
function utf_substr($str,$len){
for($i=0;$i<$len;$i++)

{

$temp_str=substr($str,0,1);

if(ord($temp_str) > 127)$i++;if($i<$len){$new_str[]=substr($str,0,3);$str =substr($str,3);}}else{$new_str[]=substr($str,0,1);$str=substr ($str,1);
}

}return join($new_str);
}
?> http://www.bkjia.com/PHPjc/444686.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444686.htmlTechArticleThis article summarizes the function to intercept strings based on the php function substr, but it appears when encountering Chinese The solution to the problem of garbled characters when half of Chinese characters are intercepted is introduced below...