利用php内置方法mb_substr截取中文不乱码,使用起来非常简单,大家参考使用吧
GBK编码截取示例
代码如下:
$str = '我是谁'; //gbk编码的字符串 echo mb_substr($str, 0, 1, 'gbk'); //输出 我
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来检验字符串编码,但还不完善。

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

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
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.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.