Home Backend Development PHP Tutorial PHP兑现中文字符的无乱码截断

PHP兑现中文字符的无乱码截断

Jun 13, 2016 am 10:49 AM
str substr

PHP实现中文字符的无乱码截断

PHP内置的substr()函数不能对中文字符进行很好的截断处理,对于一些中英文混合的字符会出现乱码的情况。下面提供两种解决函数。

?

1、GB2312编码方式的截断

?

function msubstr($str, $start, $len) {    if (strlen($str)-$start  0xa0) {   //0xa0 表示中文汉字编码的第一个编码字符ASCII 码值都大于0xa0            $tmpstr .= substr($str, $i, 2);            $i++;         } else            $tmpstr .= substr($str, $i, 1);     }     return $tmpstr . "...";} 
Copy after login

?

2、utf8格式下的中文字符截断

?

UTF-8编码的字符可能由1~3个字节组成, 具体数目可以由第一个字节判断出来。(理论上可能更长,但这里假设不超过3个字节)

第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符

第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符

否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号)。

?

//$sourcestr 是要处理的字符串//$cutlength 为截取的长度(即字数)function cut_str($sourcestr,$cutlength){   $returnstr='';   $i=0;   $n=0;   $str_length=strlen($sourcestr);//字符串的字节数   while (($n=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$cutlength){          $returnstr = $returnstr . "...";//超过长度时在尾处加上省略号      }     return $returnstr;}
Copy after login
?

?

?

?

1 楼 zeroneta 2011-09-26  
呵呵 那我就在来个 UTF-8截取无乱码
function utf8( $a, $s = '' )
{
preg_match_all( '/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/', $a, $d, PREG_PATTERN_ORDER );
return join( $s, $d[0] );
}

2 楼 zeroneta 2011-09-26  
调用方式


utf8( substr( '截取我', 0, 1 ) );

3 楼 bupt_roy 2011-09-29  
zeroneta 写道
调用方式


utf8( substr( '截取我', 0, 1 ) );

高手,学习学习,哈哈
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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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

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

Use the PHP function 'substr' to get the substring of a string Use the PHP function 'substr' to get the substring of a string Jul 24, 2023 pm 10:13 PM

Use the PHP function "substr" to obtain the substring of a string. In PHP programming, you often encounter situations where you need to obtain part of the content of a string. At this time, we can use PHP's built-in function "substr" to achieve this. This article explains how to use the "substr" function to get a substring of a string and provides some code examples. 1. Basic usage of the substr function The substr function is used to obtain a substring of a specified length from a string. Its basic syntax is as follows: substr(

PHP mb_substr function invalid solution PHP mb_substr function invalid solution Mar 22, 2024 am 09:00 AM

Solutions for invalid PHPmb_substr function When developing PHP applications, the mb_substr function is often used to intercept strings. However, sometimes you may encounter situations where the mb_substr function is invalid, mainly due to character encoding issues in different environments. In order to solve this problem, we need to effectively handle the mb_substr function. A common solution is to ensure that the mb_substr function can

PHP convert first letter of string to lowercase PHP convert first letter of string to lowercase Mar 21, 2024 pm 02:11 PM

This article will explain in detail how PHP converts the first letter of a string to lowercase. I think it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. Converting the first letter of a PHP string to lowercase Introduction In PHP, converting the first letter of a string to lowercase is a common operation. This can be achieved by using the built-in function lcfirst() or the string operator strtolower(). This guide will dive into both approaches, providing example code and best practices. Method 1: Use the lcfirst() function The lcfirst() function is specifically designed to convert the first letter of a string to lowercase while leaving the rest of the characters unchanged. Its syntax is as follows: st

See all articles