php mb_substr实现中文字符串截取无乱码的方法
mb_substr
在网上看到有很多字符截取函数大仔细研究了一下发现所有的都是针对编码问题进行处理了,下面我们看一个简化的函数,希望对大家有帮助.
注意:在windows目录下找到php.ini打开编辑,搜索mbstring.dll,找到:
;extension=php_mbstring.dll,把前面的;号去掉,这样mb_substr函数就可以生效了.
php例子代码如下:
<?php $str = '这样一来我的字符串就不会有乱码^_^'; echo "mb_substr:" . mb_substr($str, 0, 7, 'utf-8'); //结果:这样一来我的字 echo "<br>"; echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8'); //结果:这样
Copy after login
从上面的例子可以看出,mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象,代码如下:
/** * 实现中文字串截取无乱码的方法 */ function getSubstr($string, $start, $length) { if(mb_strlen($string,'utf-8')>$length){ $str = mb_substr($string, $start, $length,'utf-8'); return $str.'...';//开源代码phprm.com }else{ return $string; } }
Copy after login
教程链接:
随意转载~但请保留教程地址★
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
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

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 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks ago
By DDD
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
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
CakePHP Tutorial
1376
52

