Home php教程 PHP源码 php截取中文字符串不乱码的方法_php实例

php截取中文字符串不乱码的方法_php实例

May 25, 2016 pm 05:09 PM

利用php内置方法mb_substr截取中文不乱码,使用起来非常简单,大家参考使用吧

GBK编码截取示例

代码如下:

$str = '我是谁';  //gbk编码的字符串
echo mb_substr($str, 0, 1, 'gbk'); //输出 我
Copy after login

mb_substr方法比substr多一个参数,用来指定字符串编码。

utf-8编码截取示例

[code]
$str = '我abc是谁';  //utf-8编码的字符串
echo mb_substr($str, 0, 2, 'utf-8'); //输出 我a
[/code

中英混合也完全没有问题。

友情提示

使用的时候要注意php文件的编码,和网页显示时的编码。

使用这个mb_substr方法要事先知道字符串的编码,如果不知道编码,就需要判断,mbstring库还提供了mb_check_encoding来检验字符串编码,但还不完善。

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)