PHP interception string functions substr, iconv_substr, mb_substr examples and pros and cons analysis_PHP tutorial

WBOY
Release: 2016-07-13 10:28:35
Original
895 people have browsed it

Sample code, using the functions substr and iconv_substr, mb_substr

Copy code The code is as follows:



Intercept Chinese string_Script Home_www.jb51.net


< ?php
$str='123Script House 456 welcomes everyone';
echo substr($str,0,4);
echo '
';
echo iconv_substr ($str,0,4,'utf-8');
echo '
';
echo mb_substr($str,0,4,'utf-8');
? >


Note:
The above code uses two functions iconv_substr and mb_substr, both of which can intercept strings under the current character to achieve Chinese character interception without garbled characters.

How to choose?

1. The iconv library may not run correctly on some operating systems, and the GNU extension library needs to be installed to ensure its normal operation. The mb_substr function is more compatible.

2. The iconv function will first convert the current string into the corresponding encoding and then intercept it, while the mb function intercepts directly according to the specified encoding (providing safe multi-byte interception), so the interception efficiency of the mb function is higher.

Therefore, the mb_substr function is the most appropriate choice for intercepting Chinese strings.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/781414.htmlTechArticleSample code, using the functions substr and iconv_substr, mb_substr. Copy the code as follows: html headmeta charset='utf-8 ' title intercepts Chinese string_Script Home_www.jb51.net/title /h...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template