Home > Backend Development > PHP Tutorial > Example of string interception function mb_substr in php

Example of string interception function mb_substr in php

黄舟
Release: 2023-03-16 15:28:01
Original
1359 people have browsed it

string mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] )-Intercept string

(PHP 4 >= 4.0.6, PHP 5)
$str To get the target string of the string (the starting position of the string is 0)
$start, the first character to be used in $str Position
$length, the length of the obtained substring (note that it is not the end position)
$encoding, you can specify the character encoding (generally used when processing Chinese characters, and this problem is encountered a lot)


<?php
mb_internal_encoding ("UTF-8");    //如果把UTF-8改为编码,下面针对于中文字符串处理的值会改变。
echo mb_internal_encoding()."";    //获取字符编码为ISO-8859-1
echo mb_substr(&#39;abcdefghijk&#39;,0,9)."";    //abcdefghi
echo mb_substr(&#39;abcdefghijk&#39;,1,5)."";    //bcdef
echo mb_substr(&#39;我们都是中国人&#39;,0,9)."";    //我们都是中国人
echo mb_substr(&#39;我们都是中国人&#39;,0,9,&#39;gb2312&#39;)."";    //我们都是中
echo mb_substr(&#39;我们都是中国人&#39;,0,9,&#39;utf-8&#39;);    //我们都是中国人
?>
Copy after login

Summary:
1. When processing English strings, the fourth parameter ($encoding) of this function can be ignored.
2. When processing Chinese strings, be careful and must consider encoding issues. Different encodings have different Chinese values.
3. When Chinese strings are imported into the database, it becomes even more important.
4. The coding ability to process strings or text is a criterion for measuring a programmer's skill.

The above is the detailed content of Example of string interception function mb_substr in php. For more information, please follow other related articles on the PHP Chinese website!

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