search
HomeBackend DevelopmentPHP ProblemHow to intercept strings with php without garbled characters

In PHP, if the substr() function intercepts a Chinese string, garbled characters may appear. This is because the number of bytes occupied by one byte in Chinese and Western characters is different.

How to intercept strings with php without garbled characters

The length parameter of substr is calculated in bytes. When encoding in GB2312, a Chinese character occupies 2 bytes and an English character occupies 1 byte; while in GB2312 encoding, In UTF-8 encoding, a Chinese character may occupy 2 or 3 bytes, and English or half-width punctuation characters may occupy 1 byte. (Recommended learning: PHP programming from entry to proficiency)

Directly using the PHP function substr to intercept Chinese characters may cause garbled characters, mainly because substr may forcibly convert a Chinese character "Saw" in half. Solution:

1. Use the mb_substr interception of the mbstring extension library to avoid garbled characters.

2. Write the interception function yourself, but the efficiency is not as high as using the mbstring extension library.

3. If it is just to output the intercepted string, it can be implemented in the following way: substr($str, 0, 30).chr(0).

The substr() function can split text, but if the text to be split includes Chinese characters, you will often encounter problems. In this case, you can use the mb_substr()/mb_strcut function.

The usage of mb_substr()/mb_strcut is similar to substr(), except that one more parameter needs to be added at the end of mb_substr()/mb_strcut to set the encoding of the string, but most servers do not open php_mbstring. dll, you need to open php_mbstring.dll in php.ini.

For example:

<?php
echo mb_substr(&#39;这样一来我的字符串就不会有乱码^_^&#39;, 0, 7, &#39;utf-8&#39;);
?>

Output: This way my words

<?php
echo mb_strcut(&#39;这样一来我的字符串就不会有乱码^_^&#39;, 0, 7, &#39;utf-8&#39;);
?>

Output: This way一

As can be seen from the above example, mb_substr splits characters by words, while mb_strcut splits characters by bytes, but neither will produce half a character.

The above is the detailed content of How to intercept strings with php without garbled characters. 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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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