php mb_substr()函数截取中文字符串应用示例,
substr()函数用来截取字符串,但是对于中文字符会出现问题,而mb_substr()和mb_strcut这两个函数可以,用法与substr()相似,只是在函数最后要加入多一个参数,以设定字符串的编码,使用这两个函数需要在php.ini中把php_mbstring.dll打开。
<?php header("content-type:text/html; charset=utf-8"); $string = "你好我好大家好"; echo strlen($string).'</br>'; echo mb_substr($string,0,4,'utf-8').'...</br>'; echo mb_strcut($string,0,4,'utf-8').'...'; ?>
输出的结果:
21
你好我好...
你...
从上面的例子可以看出,mb_substr是按字来分割字符,而mb_strcut是按字节来分割字符,但是都不会产生半个字符的现象。

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
