Home > Backend Development > PHP Problem > What to do if php Chinese intercepts garbled characters

What to do if php Chinese intercepts garbled characters

藏色散人
Release: 2023-03-07 16:52:02
Original
2042 people have browsed it

Solution to php Chinese interception of garbled characters: First obtain the length of the Chinese string through the mb_strlen function; then use PHP's built-in function mb_substr to intercept the string.

What to do if php Chinese intercepts garbled characters

The operating environment of this tutorial: windows10 system, php5.6. This article is applicable to all brands of computers.

Recommendation: "PHP Video Tutorial"

Solution to garbled Chinese character strings when intercepted by PHP

needs to be intercepted For string requirements, the first thing that comes to mind is substr(), but there will be problems with garbled characters. So if you want to intercept a Chinese string, you can use PHP's built-in function Chinese interception: mb_substr(), which is

mb_substr( $str, $start, $length, $encoding )
Copy after login

$str: Chinese string that needs to be intercepted;

$start: interception start parameter, starting from 0;

$length: the number of words to be intercepted;

$ encoding: Web page encoding, such as UTF-8, GB2312, GBK;

In addition, a function to obtain the length of the Chinese string is attached to obtain the Chinese length: mb_strlen()

mb_strlen( $str, $encoding )
$str : 中文字符串 ;
$encoding : 网页编码,如UTF-8,GB2312,GBK;
Copy after login

The above is the detailed content of What to do if php Chinese intercepts garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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