Table of Contents
2、截取GB2312以及GBK编码的字符串" >2、截取GB2312以及GBK编码的字符串
3、截取utf8或GB2312或者GBK编码的字符串" >3、截取utf8或GB2312或者GBK编码的字符串
Home Backend Development PHP Tutorial 完善解决截取中文汉字不乱码-PHP字符串函数(支持utf8、GBK、GB2312)

完善解决截取中文汉字不乱码-PHP字符串函数(支持utf8、GBK、GB2312)

Jun 13, 2016 am 10:47 AM
start str substr

完美解决截取中文汉字不乱码-PHP字符串函数(支持utf8、GBK、GB2312)

我们主要对utf8、GBK、GB2312的截取中文字符串进行讲解,完美解决截取中文汉字不乱码-PHP字符串函数:

1、截取GB2312以及GBK编码的字符串

函数功能:截取GB2312以及GBK编码的字符串,从第一个字符开始截取,2个长度代表一个汉字
$str————截取源字符串
$len————截取长度(2代表一个汉字)

PS:该函数不能用于utf8编码字符串,会出现乱码

function splitStr($str,$len)
{
if($len{
return false;
}
else
{
$sLen=strlen($str);
if($len>=$sLen)
return $str;
else
{
for($i=0;$i{
if(ord(substr($str,$i,1))>0xa0)
$i++;
}

if($i>=$len)
return substr($str,0,$len);
elseif(ord(substr($str,$i,1))>0xa0)
return substr($str,0,$len-1);
else
return substr($str,0,$len);
}
}
}

2、截取GB2312以及GBK编码的字符串

函数功能:截取GB2312以及GBK编码的字符串,可以设置截取位置和长度,2个长度代表一个汉字
$str————截取源字符串
$start———-起始位置,不能为空,从1算起
$len————截取长度(2代表一个汉字),如果为空则截取到字符串末尾

PS:该函数不能用于utf8编码字符串,会出现乱码

function substr_for_gb2312($str,$start,$len=null)
{
$totlelength = strlen($str);

//特例情况
if ($len == null) $len = $totlelength;
if ($len ==0) return “”;
if ($len >= $totlelength && $start == 0 ) return $str;
if ($start > $totlelength) return “”;

//分析$start
if ($start 0时的定位.
{
if ( abs($start) >= $totlelength )
$start = 0;
else
$start = $totlelength – abs($start);
}

//确定起始位置,当起始位拆分某汉字时,返回值包含此汉字.
if ($start > 0)
{
$i = $start-1;
$flag = -1;
while ($i >= 0)
{
if ( ord(substr($str,$i,1)) > 160)
{
$flag = -1*$flag;
}
else break;
$i–;
}
if($flag==1)
{
$start = $start – 1;
$len++; //保证不位移.
}
}

$str = substr($str,$start);//截除字符串$str的$start位前的字符
$totlelength = strlen($str);

//确定结束位置,当结束位拆分某汉字时,返回值不包含此汉字.
if ($lenif ($len $i=min($len,$totlelength);
$i–;
$flag = -1;
while ($i >= 0)
{
if (ord(substr($str,$i,1))>160)
{
$flag=-1*$flag;
}
else break;
$i–;
}

if($flag == 1)?? ?$len=$len-1;
$subit=substr($str,0,$len);

return $subit;
}

3、截取utf8或GB2312或者GBK编码的字符串

函数功能:截取utf8或GB2312或者GBK编码的字符串,从第一个字符开始截取,1个长度代表一个汉字
$sourcestr————截取源字符串
$cutlength————截取长度(字数)

PS:这个函数很万能,但是相对前两个耗资源一些

function substr_for_utf8($sourcestr,$cutlength)
{
$returnstr=”;
$i=0;
$n=0;
$str_length=strlen($sourcestr);?? ?//字符串的字节数
while (($n{
$temp_str=substr($sourcestr,$i,1);
$ascnum=Ord($temp_str); //得到字符串中第$i位字符的ascii码
if ($ascnum>=224) //如果ASCII位高与224,
{
$returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范,将3个连续的字符计为单个字符
$i=$i+3; //实际Byte计为3
$n++; //字串长度计1
}
elseif ($ascnum>=192)//如果ASCII位高与192,
{
$returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范,将2个连续的字符计为单个字符
$i=$i+2; //实际Byte计为2
$n++; //字串长度计1
}
elseif ($ascnum>=65 && $ascnum{
$returnstr=$returnstr.substr($sourcestr,$i,1);
$i=$i+1; //实际的Byte数仍计1个
$n++; //但考虑整体美观,大写字母计成一个高位字符
}
else //其他情况下,包括小写字母和半角标点符号,
{
$returnstr=$returnstr.substr($sourcestr,$i,1);
$i=$i+1;?? ?//实际的Byte数计1个
$n=$n+0.5;?? ?//小写字母和半角标点等与半个高位字符宽…
}
}

if ($str_length>$cutlength)
{
$returnstr = $returnstr . “…”;?? ?//超过长度时在尾处加上省略号
}

return $returnstr;
}

最后,你可以简单写个程序调用一下试试,例如:

$a=”我们都会写Hello world!这个最简单的程序。”;
echo $a.”
”;
$a=substr_for_utf8($a,4);
echo $a.”
”;
?>

怎么样?不错吧,那就快来试试吧 O(∩_∩)O哈哈~

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if docker start cannot start? What should I do if docker start cannot start? Oct 21, 2022 pm 03:43 PM

Solution to docker start failure: 1. Check the running status, and then release the occupied memory through the "echo 3 > /proc/sys/vm/drop_caches" command; 2. Use "$netstat -nltp|grep .. ." command to check whether the port has been occupied. If it is found to be occupied after going online, change it to an available port and restart.

PHP returns the ASCII value of the first character of the string PHP returns the ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values ​​are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

What to do if node start reports an error What to do if node start reports an error Dec 29, 2022 pm 01:55 PM

Solution to node start error: 1. Execute "node xx.js" directly in the terminal; 2. Add start startup item "scripts": {"test": "echo \"Error: no test specified\" && exit 1 ","start":"node service.js"}"; 3. Re-execute "npm start".

Python built-in type str source code analysis Python built-in type str source code analysis May 09, 2023 pm 02:16 PM

1The basic unit of Unicode computer storage is the byte, which is composed of 8 bits. Since English only consists of 26 letters plus a number of symbols, English characters can be stored directly in bytes. But other languages ​​(such as Chinese, Japanese, Korean, etc.) have to use multiple bytes for encoding due to the large number of characters. With the spread of computer technology, non-Latin character encoding technology continues to develop, but there are still two major limitations: no multi-language support: the encoding scheme of one language cannot be used in another language and there is no unified standard: for example There are many encoding standards in Chinese such as GBK, GB2312, GB18030, etc. Since the encoding methods are not unified, developers need to convert back and forth between different encodings, and many errors will inevitably occur.

What are the similarities and differences between __str__ and __repr__ in Python? What are the similarities and differences between __str__ and __repr__ in Python? Apr 29, 2023 pm 07:58 PM

What are the similarities and differences between __str__ and __repr__? We all know the representation of strings. Python's built-in function repr() can express objects in the form of strings to facilitate our identification. This is the "string representation". repr() obtains the string representation of an object through the special method __repr__. If __repr__ is not implemented, when we print an instance of a vector to the console, the resulting string may be. >>>classExample:pass>>>print(str(Example()))>>>

Understand the substr() function in PHP to intercept strings Understand the substr() function in PHP to intercept strings Nov 18, 2023 am 11:27 AM

Understand the substr() function in PHP for intercepting strings. In the PHP language, the substr() function is a very useful string processing function, which can be used to intercept string fragments at a specified position and length. The substr() function accepts three parameters: the string to be intercepted, the starting position of the interception, and the length of the interception. Below we will introduce the use of the substr() function in detail and give specific code examples. Basic usage of substr() function substr() function

How to use the start method and run method in Java thread How to use the start method and run method in Java thread Apr 20, 2023 am 08:58 AM

start method and run method The $start()$ method is used to start a thread. At this time, the thread is in the ready (runnable) state and is not running. Once the $cpu$ time slice is obtained, the $run()$ method starts to be executed. . Directly calling the $run()$ method only calls a method in a class, which is essentially executed in the current thread. Therefore, it can only be achieved by using the $start()$ method to call the $run()$ method. True multithreading. Sample code@Slf4j(topic="c.Test4")publicclassTest4{publicstaticvoidmain(Strin

See all articles