PHP substr 截取中文乱码的问题解决办法
在php中为我们提供了几个字符截取函数,包括substr,mb_substr,mb_strcut函数,我们有些php初学者会利用substr来截取中文,结果发现中文会有乱码了,出现乱码我们可以使用mb_substr来解决。
文章页面的 description 是使用 substr 函数来截取220字符的,但是最后一个汉字总是乱码,而且截取出来的长度也不正确。
通过神奇的 Google 找到方法,可能是因为 substr(string,start,length),会将汉字以字符的形式截断,而造成乱码
解决方案:
使用 PHP 扩展库中的 mb_substr 方法。
注意
1.确保你的Windows/system32下有php_mbstring.dll这个文件,没有就从你Php安装目录extensions里拷入Windows/system32里面。
2.在windows目录下找到php.ini打开编辑,搜索mbstring.dll,找到
;extension=php_mbstring.dll把前面的;号去掉,这样mb_substr函数就可以生效了
方法定义:
string mb_substr ( string str, int start [, int length [, string encoding]] )
注意:在使用 mb_substr()/mb_strcut 最后要加入多一个参数,以设定字符串的编码,
例如:
代码如下 | 复制代码 |
echo mb_substr(‘原本会出现乱码的汉字!’, 0, 7, ‘utf-8′); |
再如:
代码如下 | 复制代码 |
$description = mb_substr(strip_tags($post->post_content),0,220,’utf-8′); |
mb_strcut函数
mb_strcut函数功能也可以截取字符串长度,下面实例具体看看区别在哪:
代码如下 | 复制代码 |
$str = '这样一来我的字符串就不会有乱码^_^'; echo "mb_substr:" . mb_substr($str, 0, 7, 'utf-8'); echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8'); |
从上面的例子可以看出,mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象。
substr()函数中文版 普通的substr()函数可以取得字符串的指定长度子字符串,但遇到中文时可能会在新字符串末尾产生乱码,下面这个函数将超过$len长度的字符串转换成以“...”结尾,并且去除了乱码。
用法:$new = getsubstring($old,20);
代码如下 | 复制代码 |
function getsubstring($str,$len) { for($i = 0;$i { if ($i >=0 AND $i { if(ord(substr($str,$i,1)) > 0xa1) $result_str.=substr($str,$i,2); else $result_str.=substr($str,$i,1); } if(ord(substr($str,$i,1)) > 0xa1) $i++; } if(strlen($str) return $result_str; else return $result_str."..."; } |

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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.
