Introduction to php processing Chinese string interception (mb_substr) and obtaining the number of characters in Chinese strings

不言
Release: 2023-04-02 09:02:02
Original
11072 people have browsed it

PHP Chinese processing Chinese string interception (mb_substr) and obtaining the number of Chinese string characters, friends in need can refer to it.

1. Chinese interception: mb_substr()

mb_substr( $str, $start, $length, $encoding )

$str, required Truncated string
$start, the starting point of truncation, the starting point is 0
$length, the number of words to be intercepted
$encoding, web page encoding, such as utf-8, GB2312, GBK

Example:

<?php 
$str=&#39;php中文网://www.php.cn&#39;; 
echo mb_substr($str,0,4,&#39;utf-8&#39;);//截取头5个字,假定此代码所在php文件的编码为utf-8 
?>
Copy after login

The result shows: php Chinese website

2. Get the Chinese length: mb_strlen()

mb_strlen($ str, $encoding )

$str, the string to calculate the length
$encoding, web page encoding, such as utf-8, GB2312, GBK

Example:

<?php 
$str=&#39;php中文网://www.php.cn&#39;; 
echo mb_strlen($str,&#39;utf-8&#39;);//假定此代码所在php文件的编码为utf-8 
?>
Copy after login

The result shows: 24

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PHP encapsulated curl calling interface and introduction to common functions

PHP rounding function: ceil , Introduction to the differences between floor, round and intval

The above is the detailed content of Introduction to php processing Chinese string interception (mb_substr) and obtaining the number of characters in Chinese strings. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!