Home php教程 php手册 PHP中使用substr()截取字符串出现中文乱码问题该怎么办

PHP中使用substr()截取字符串出现中文乱码问题该怎么办

Jun 06, 2016 pm 07:41 PM
php

本文给大家介绍使用php substr()截取字符串出现乱码问题该怎么办,涉及到php substr()方法的一些知识点,感兴趣的朋友一起学习下吧

在PHP程序开发中,经常会执行字符串的截取操作,比如输出信息列表时,标题不宜过长,打印文章摘要时,也要执行一系列的字符串截取操作。遇到这些需求时,我们经常会想到使用substr()方法来实现,substr()对全英文字符串的截取是比较适合的。

但字符串只要出现中文字符,就有可能导致PHP substr中文乱码,因为中文UTF-8编码,每个汉字占3字节,而GB2312占2字节,英文占1字节,截取位数不准确,substr()硬生生地将一个中文字符“锯”成两半,造成断开的字符会把其后的..拉过来一起做一个字,所以出现了PHP substr中文乱码。

substr --- 取得部份字符串

语法 : string substr (string string, int start [, int length])

说明 :

substr( )传回 string的一部份字符串,由参数 start和 length指定。

如果 start是正数,传回的字符串将会从 string的第 start个字元开始。

Example :


如果 start是负数,传回的字符串将会从 string结尾的第 start个字开始。

Example :

如果有给予参数 length而且是正数时,传回的字符串将会从 start传回 length个字元。
如果有给予参数 length而且是负数时,传回的字符串将会结束于 string结尾的第 length个字元。

Example :

对于英文没有问题,我们测试一个中文

这种截取字符的结果,肯定不是我们想要的结果,这种出现PHP substr中文乱码的情况,可能会导致程序无法正常运行。解决办法主要有两种:

一、使用mbstring扩展库的mb_substr()截取就不会出现乱码了。

可以用mb_substr()/mb_strcut()这个函数,mb_substr()/mb_strcut()的用法与substr()相似,只是在mb_substr()/mb_strcut最后要加入多一个参数,以设定字符串的编码,但是一般的服务器都没打开php_mbstring.dll,需要在php.ini在把php_mbstring.dll打开。

如果未指定最后一个编码参数,会是三个字节为一个中文,这就是utf-8编码的特点,若加上utf-8字符集说明,所以,是以一个字为单位来截取的。

使用的时候要注意php文件的编码,和网页显示时的编码。使用这个mb_substr方法要事先知道字符串的编码,如果不知道编码,就需要判断,mbstring库还提供了mb_check_encoding来检验字符串编码,但还不完善。

PHP 自带几种字符串截取函数,其中常用到的就是 substr 和 mb_substr。前者在处理中文时,GBK 为 2 个长度单位,UTF 为 3 个长度单位,后者指定编码后,一个中文即为 1 个长度单位。

substr 有时会截 1/3 个中文或半个中文,会显示乱码,相对来说 mb_substr 更适合我们使用。不过有时候 mb_substr 就显得不那么好用了。例如我要显示一个小图片的简要信息,5 个中文正好,超过 5 个就截取前4再加上 ”…”,这样处理中文是没问题了,可是处理英文或数字,这样截取就太短了。

二、自己书写截取函数,但效率不如用mbstring扩展库来得高。下面是ecshop里面的截取UTF-8编码下字符串的函数。

function sub_str($str, $length = , $append = true) { $str = trim($str); $strlength = strlen($str); if ($length == || $length >= $strlength) { return $str; //截取长度等于或大于等于本字符串的长度,返回字符串本身 } elseif ($length

好了,本文全部叙述到此为止,相信会给大家带来收获的,以上代码也很简单,有不明白的地方欢迎给我留言,,我会在第一时间给大家答复的。

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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 vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles