Home >Backend Development >PHP Tutorial >PHP Chinese string interception (mb_substr) example

PHP Chinese string interception (mb_substr) example

WBOY
WBOYOriginal
2016-07-25 09:04:24918browse
  1. $str='Scripting School: http://bbs.it-home.org';
  2. echo mb_substr($str,0,4,'utf-8');// Intercept the first 5 characters, assuming that the encoding of the php file where this code is located is utf-8
  3. ?>
Copy the code

Output: Programmer's Home

2. Get the Chinese length: mb_strlen() mb_strlen( $str, $encoding )

$str, the string whose length is to be calculated $encoding, web page encoding, such as utf-8, GB2312, GBK

Example 2:

  1. $str='Programmer's Home: http://bbs.it-home.org';
  2. echo mb_strlen($str,'utf-8');//Assumption The encoding of the php file where this code is located is utf-8
  3. ?>
Copy the code

Output: 24



Statement:
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