


Usage and differences of substr(), mb_substr() and mb_strcut functions_PHP tutorial
In PHP, the three functions substr(), mb_substr() and mb_strcut are all character interception functions. However, when substr intercepts Chinese characters, there will be garbled characters. The latter two support Chinese interception. Let me introduce them below.
substr() function
substr(string,start,length)
String represents the object to be intercepted, start represents the position from which to intercept, 0 represents the beginning, a positive number represents the interception from the end of this number, and a negative number represents the interception position starting from the end, but still from left to Right truncation, length indicates the truncation length. A negative number indicates how many characters at the end are excluded or ignored. For example:
代码如下 | 复制代码 |
$siteurl = 'www.bKjia.c0m'; print_r (substr($siteurl,4));exit; |
//Return: bKjia.c0m means starting from the 4th character from the beginning and returning all subsequent characters.
The code is as follows | Copy code | ||||
|
If you want to intercept double-byte Chinese characters, you need to use the PHP mb_substr function or mb_strcut function, but these two functions rely on the PHP extension php_mbstring.dll component, so you need to configure your server. That is, put the php_mbstring in the PHP installation directory Copy the .dll file to the Windows/system32 directory of the c drive of your Windows 2003.
代码如下 | 复制代码 |
echo mb_substr('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8'); |
For example:
The code is as follows | Copy code | ||||
|
代码如下 | 复制代码 |
echo mb_substr('飞花院博客feihuayuan',0,9); echo mb_substr('飞花院博客feihuayuan',0,9,'utf-8'); mb_strcut('飞花院博客feihuayuan',0,9,'utf-8'); |
The code is as follows | Copy code |
echo mb_strcut('This way my string will not be garbled^_^', 0, 7, 'utf-8');<🎜> ?> |
The code is as follows | Copy code |
echo mb_substr('feihuayuan blog feihuayuan',0,9);<🎜> //Return to: Feihuayuan<🎜> <🎜>echo mb_substr('feihuayuan blog feihuayuan',0,9,'utf-8');<🎜> //Return: Feihuayuan Blog feih<🎜> <🎜>mb_strcut('Feihuayuan blog feihuayuan',0,9,'utf-8');<🎜> Then return: Feihuayuan<🎜> ?> |
For another example, there is a piece of text that is segmented using mb_substr and mb_strcut respectively:
PLAIN TEXT
CODE:
The code is as follows
|
Copy code
|
||||
$str = 'I am a relatively long string of Chinese-'; echo "mb_substr:" . mb_substr($str, 0, 6, 'utf-8');
echo " echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8'); ?> |
The output results are as follows:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Created by Ripple, Ripple is used for cross-border payments, which are fast and low-cost and suitable for small transaction payments. After registering a wallet and exchange, purchase and storage can be made.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.
