PHP 中文字符串截取无乱码的步骤以及php汉字截取函数_preg_split()
PHP 中文字符串截取无乱码的方法以及php汉字截取函数_preg_split()
昨天,在写代码时我遇到了一个中文字符串截取的问题,原本是想直接用substr()来,但这个是在单字节符下用的,对于多字节编码的汉字或是其它语言字符中,这个函数可能就用不上了,而且会容易截出乱码来!
问题产生及使用案例网站:天气预报15天查询(http://tqybw.net)
问题时间:2013-10-31
解决思路:把汉字按一个汉字在编码下按其在完整性正则分割成数组,再根据需要取截取长度;
1、实现方法函数
<?php header('content-Type:text/html:charset=utf-8'); function substr_chinese($str, $start, $length = null) { return join("", array_slice( preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $start, $length) ); } (PS:^_^不错的php学习交流群:276167802,验证:csl,谢谢!) //实例 $str = "中国地图全图"; echo substr_utf8($str, 0, 4); //输出: 中国地图 ?>
2、主要函数功能说明preg_split:
对于初学者来说,大家对 说明 array preg_split ( string $pattern, string $subject [, int $limit [, int $flags]] )
返回一个数组,包含 subject 中沿着与 pattern 匹配的边界所分割的子串。
如果指定了 limit,则最多返回 limit 个子串,如果 limit 是 -1,则意味着没有限制,可以用来继续指定可选参数 flags。
flags 可以是下列标记的任意组合(用按位或运算符 | 组合):
PREG_SPLIT_NO_EMPTY
如果设定了本标记,则 preg_split() 只返回非空的成分。
PREG_SPLIT_DELIM_CAPTURE
如果设定了本标记,定界符模式中的括号表达式也会被捕获并返回。本标记添加于 PHP 4.0.5。
PREG_SPLIT_OFFSET_CAPTURE
如果设定了本标记,如果设定本标记,对每个出现的匹配结果也同时返回其附属的字符串偏移量。注意这改变了返回的数组的值,使其中的每个单元也是一个数组,其中第一项为匹配字符串,第二项为其在 subject 中的偏移量。本标记自 PHP 4.3.0 起可用。
如果不需要正则表达式的功能,可以选择使用更快(也更简单)的替代函数如 explode() 或 str_split()。
3、array_slice说明:
array_slice() 函数在数组中根据条件取出一段值,并返回。
<?php $a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); print_r(array_slice($a,1,2)); ?> 输出:Array ( [0] => Cat [1] => Horse )
本文讲述了PHP 中文字符串截取无乱码方法,希望本文能给读者带来灵感,帮助读者解决疑问,感谢阅读本文。

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

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

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

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

In this chapter, we are going to learn the following topics related to routing ?

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

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

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