Home >Backend Development >PHP Tutorial >Examples of php intercepting Chinese strings and getting the number of characters in Chinese strings

Examples of php intercepting Chinese strings and getting the number of characters in Chinese strings

黄舟
黄舟Original
2017-11-15 09:36:051928browse

In the previous article, we introduced an example of intercepting characters in PHP. We only introduced the string of intercepting integer . So what about the example where we want to intercept Chinese strings? To achieve it, today I will introduce to you an example of intercepting Chinese strings and obtaining the number of characters in Chinese strings in PHP!

1. Chinese interception: mb_substr()

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

$str, the string that needs to be truncated
$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:

The code is as follows:

<?php 
$str=&#39;中文网://m.sbmmt.com&#39;; 
echo mb_substr($str,0,3,&#39;utf-8&#39;);//截取头3个字,假定此代码所在php文件的编码为utf-8 
?>

The result shows: 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:

The code is as follows:

<?php 
$str=&#39;脚本之家:http://www.jb51.net&#39;; 
echo mb_strlen($str,&#39;utf-8&#39;);//假定此代码所在php文件的编码为utf-8 
?>

The result shows: 24

Summary:

Through the study of this article, we know that php intercepts strings The integer strings are completely different from the use function of Chinese strings. Friends, please don’t make a mistake!

Related recommendations:

Introduction to the method of intercepting strings in php

PHP intercepts string function substr() function example usage detailed explanation

php intercepts string (no garbled utf8)

The above is the detailed content of Examples of php intercepting Chinese strings and getting the number of characters in Chinese strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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