Home Backend Development PHP Tutorial Application of mb_substr function to solve the problem of garbled Chinese characters intercepted by PHP

Application of mb_substr function to solve the problem of garbled Chinese characters intercepted by PHP

Jun 10, 2017 am 10:05 AM

1. First, make sure you have the file php_mbstring.dll in Windows/system32. If not, copy it from your Php installation directory extensions into Windows/system32.
2. Find php.ini in the windows directory, open it for editing, search for mbstring.dll, find
;extension=php_mbstring.dll, and remove the ; sign in front, so that the mb_substr function can take effect. The mb_strcut function can also intercept strings. Length, let’s see the difference in detail in the following example:

The code is as follows:

<?php 
$str = &#39;这样一来我的字符串就不会有乱码^_^&#39;; 
echo "mb_substr:" . mb_substr($str, 0, 7, &#39;utf-8&#39;); 
//结果:这样一来我的字 
echo "
"; 
echo "mb_strcut:" . mb_strcut($str, 0, 6, &#39;utf-8&#39;); 
//结果:这样 
?>
Copy after login

As can be seen from the above example, mb_substr splits characters by words, while mb_strcut splits characters by bytes, but both There will be no half-character phenomenon.

The above introduces the application of the mb_substr function, a solution to the problem of intercepting garbled Chinese characters in PHP. I hope it will be helpful to friends who are interested in PHP tutorials.

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if php mb_substr does not execute? What should I do if php mb_substr does not execute? Feb 21, 2023 am 09:41 AM

Solution to php mb_substr not executing: 1. Find and open the php.ini configuration file; 2. Find the "extension=mbstring" item, and then delete the semicolon in front of the item; 3. Save the modifications to the php.ini file. Can.

Methods to optimize Java string interception performance Methods to optimize Java string interception performance Jun 29, 2023 pm 10:51 PM

Title: How to optimize string interception performance in Java development In Java development, string interception is a common operation. However, due to the immutability of strings and the underlying implementation mechanism, string interception may cause performance issues. Therefore, we need to pay attention to optimizing string interception methods in daily development to improve program performance. Use appropriate parameters when using the String.substring() method. The substring() method in the String class is a commonly used string interception method.

PHP regular expression: How to extract substrings between multiple specific characters from a string and concatenate them into a string PHP regular expression: How to extract substrings between multiple specific characters from a string and concatenate them into a string Jun 23, 2023 am 08:51 AM

PHP regular expressions are a powerful tool that can be used to match and extract specific text from strings. In this article, we will introduce how to use PHP regular expressions to extract substrings between multiple specific characters in a string and concatenate them into a string. Before we begin, let's take a look at some basic concepts in regular expressions: Pattern: the regular expression used for matching. For example, if we want to match all numbers in a string, then we can use the pattern: /d/charset: used to match a set of characters

Use Java's String.substring() function to intercept the substring of a string Use Java's String.substring() function to intercept the substring of a string Jul 25, 2023 pm 09:06 PM

Use java's String.substring() function to intercept a substring of a string. In the Java programming language, the String class provides a wealth of methods for manipulating strings. Among them, the String.substring() function is a commonly used method that can be used to intercept substrings of strings. This article will introduce how to use the String.substring() function to intercept strings, and provide some code examples for practical application scenarios. String.subst

Understand the basic usage and examples of PHP mb_substr function Understand the basic usage and examples of PHP mb_substr function Jun 27, 2023 pm 12:47 PM

PHP's mb_substr function is a powerful tool for intercepting strings. When you need to extract a part of the content from a long string, the mb_substr function can help you do it quickly. The usage of the mb_substr function is very simple, it only requires three parameters. The first parameter is the string that needs to be intercepted, the second parameter is the starting position of interception, and the third parameter is the length that needs to be intercepted. Let’s look at an example: $str="HelloWorld!&q

How to use the substring() function of the String class in Java to implement string interception How to use the substring() function of the String class in Java to implement string interception Jul 25, 2023 am 08:19 AM

How does Java use the substring() function of the String class to implement string interception? In Java programming, the String class provides many methods for processing strings. Among them, the substring() function is a very commonly used function, which can implement the interception operation of strings. In this article, I will introduce how to use the substring() function to implement string interception and provide some code examples. First, let us understand the substring() function

Complete list of PHP string interception methods: mb_substr() is no longer applicable Complete list of PHP string interception methods: mb_substr() is no longer applicable Mar 15, 2024 pm 06:09 PM

In PHP, intercepting strings is one of the common operations, and one of the most commonly used functions is the mb_substr() function. However, as the PHP version is updated, the mb_substr() function may no longer be applicable in some cases, which may cause character encoding issues or performance issues. Therefore, this article will introduce some methods to implement string interception in PHP, as well as specific code examples to help developers better understand and apply these methods. 1. Use the mb_substr() function to intercept words

How to use substr function to intercept string in PHP How to use substr function to intercept string in PHP Jun 26, 2023 pm 12:47 PM

In PHP, processing strings is a very common task. Sometimes we need to intercept part of a string, then we can use the substr() function. The substr() function is a powerful string interception function that can help us intercept the required part from a string. The syntax of the substr function is as follows: substr(string$string,int$start,int|null$length=null):strin

See all articles