Home php教程 php手册 php mb_substr()函数截取中文字符串应用示例

php mb_substr()函数截取中文字符串应用示例

Jun 06, 2016 pm 08:19 PM
mb_substr intercept

substr()函数用来截取字符串,但是对于中文字符会出现问题,而mb_substr()和mb_strcut这两个函数可以,下面为大家介绍其具体用法

substr()函数用来截取字符串,但是对于中文字符会出现问题,而mb_substr()和mb_strcut这两个函数可以,用法与substr()相似,只是在函数最后要加入多一个参数,以设定字符串的编码,使用这两个函数需要在php.ini中把php_mbstring.dll打开。

'; echo mb_substr($string,0,4,'utf-8').'...'; echo mb_strcut($string,0,4,'utf-8').'...'; ?>

输出的结果:

21
你好我好...
你...

从上面的例子可以看出,mb_substr是按字来分割字符,而mb_strcut是按字节来分割字符,但是都不会产生半个字符的现象。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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.

Detailed explanation of string interception method in Go language Detailed explanation of string interception method in Go language Mar 13, 2024 am 08:03 AM

Detailed explanation of string interception methods in Go language. In Go language, strings are immutable byte sequences, so some methods need to be used to implement string interception. String interception is a common operation to obtain a specific part of a string. You can intercept the first few characters, the last few characters of the string, or a certain length of characters from a specific position according to your needs. This article will introduce in detail how to intercept strings in Go language and provide specific code examples. Using slicing to implement string interception In Go language, you can use slicing to

How to use ROUND function to intercept decimal places in MySQL How to use ROUND function to intercept decimal places in MySQL Jul 13, 2023 pm 09:21 PM

How to use the ROUND function in MySQL to intercept the number of decimal places. In MySQL, you can use the ROUND function to intercept the number of decimal places. The ROUND function rounds a number to a specified number of decimal places. The following will introduce you to the use of ROUND function in detail and provide code examples. Syntax: ROUND(X,D)X represents the number to be rounded, and D represents the number of decimal places to be retained. Example of using the ROUND function to intercept the number of decimal places: Suppose there is a table named produc

PHP Chinese string interception skills: Say goodbye to mb_substr() PHP Chinese string interception skills: Say goodbye to mb_substr() Mar 15, 2024 pm 12:18 PM

In PHP development, we often encounter situations where Chinese strings need to be intercepted. Traditionally, we usually use the mb_substr() function to handle the interception of Chinese characters, but its performance is poor and not readable enough. This article will introduce some new Chinese string interception techniques, let us say goodbye to mb_substr(), and improve code efficiency and readability. Use regular expressions to intercept Chinese strings. Using regular expressions to intercept Chinese strings is an efficient and concise method. We can match Chinese characters through regular expressions

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

Quickly master the string interception function in Go language Quickly master the string interception function in Go language Mar 12, 2024 pm 06:15 PM

Quickly master the string interception function in Go language. Go language is a programming language that has attracted much attention in recent years. It has the characteristics of simplicity and efficiency, and is favored by more and more developers. In the Go language, string processing is a very common operation, and the string interception function is an important part of it. This article will use specific code examples to help you quickly master the string interception function in Go language. 1. Basic string interception. Strings in Go language can be intercepted through index. The code example is as follows: package

How UniApp implements image uploading and cropping How UniApp implements image uploading and cropping Jul 06, 2023 am 10:01 AM

UniApp is a cross-platform application development framework based on Vue.js, which can quickly develop applications for both iOS and Android platforms. In UniApp, uploading and cropping images is a common requirement. This article will introduce how to implement image uploading and cropping in UniApp, and provide corresponding code examples. 1. How to implement image upload: Use the uni.uploadFile() method to upload images. First, you need to configure uni.uploa

How to optimize string interception and splicing performance in Java development How to optimize string interception and splicing performance in Java development Jun 29, 2023 pm 06:04 PM

How to optimize the performance of string interception and splicing in Java development. In daily Java development, string interception and splicing are very common operations. However, due to the immutable nature of strings in Java, frequent string interception and splicing operations may cause performance degradation. In order to improve the performance of string interception and splicing in Java development, we can adopt the following optimization strategies. Concatenation using StringBuilder or StringBuffer: In Java, String

See all articles