Home php教程 php手册 PHP substr 截取中文乱码的问题解决办法

PHP substr 截取中文乱码的问题解决办法

Jun 13, 2016 am 09:46 AM
php substr Chinese Garbled characters several function Method exist character us intercept supply of solve question

在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 "
";

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."...";
}
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
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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles