search
HomeDaily ProgrammingPHP KnowledgeHow to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video)

This article mainly introduces to you the detailed explanation of PHP's method of intercepting Chinese string and without garbled characters.

In the process of learning PHP knowledge, PHP intercepting strings should be a very common basic string operation. I believe everyone is familiar with this knowledge point.

But some novice friends may have encountered garbled characters when intercepting Chinese and English strings. In fact, this is also very easy to solve.

First we need to understand how many bytes Chinese and English occupy.

ASCII code: A Chinese character occupies two bytes of space.

UTF-8 encoding: One Chinese character (including traditional Chinese) is equal to three bytes.

Unicode encoding: One Chinese character (including traditional Chinese) is equal to two bytes.

Below we will use a few simple code examples to introduce in detail the relevant knowledge about PHP intercepting Chinese and English strings without garbled characters.

1. About the substr function to intercept the string

<?php
echo substr("PHP中文网", 0, 5);

substr: Returns the substring of the string.

The first parameter in substr() indicates the string to be intercepted, the second parameter indicates interception starting from position 0, and the third parameter indicates the interception length.

Interception of the first 5 bytes of "PHP Chinese Network", the results are as follows:

How to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video)

As shown in the figure, garbled characters are displayed, which means that when we use the substr function When intercepting Chinese and English strings, garbled characters will appear.

2. About the mb_substr function to intercept strings

<?php
echo mb_substr("PHP中文网", 0, 5);

mb_substr: Get part of the string.

The first five characters of "PHP Chinese Network" are intercepted, and the results are as follows:

How to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video)

The first five characters are intercepted as shown in the figure, and they do not appear. Garbled characters.

Note: mb_substr intercepts the string based on the number of characters.

3. About the mb_strcut function to intercept the string

<?php
echo mb_strcut("PHP中文网", 0, 7);

Intercept the first 7 bytes of "PHP Chinese Network", the results are as follows:

How to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video)

As you can see from the picture, we want to intercept 7 bytes, but only the 6 bytes of "PHP" are intercepted. Since one Chinese character is equal to three bytes, the seventh byte here will not be displayed.

To sum up, if you encounter the need to intercept Chinese strings without garbled characters, you can choose the latter two methods (mb_substr() and mb_strcut()).

This article is an introduction to relevant knowledge about PHPChinese string interception without garbled characters. I hope it will be helpful to friends in need!

If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial. Everyone is welcome to learn and refer to it.

The above is the detailed content of How to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video). 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

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