完美解决截取中文汉字不乱码-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哈哈~

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment