How to implement Chinese string interception in php

青灯夜游
Release: 2023-03-11 16:34:01
Original
6813 people have browsed it

In PHP, you can use the mb_substr() function to intercept a Chinese string. This function can intercept a specified part of a string. It is not only valid for English characters, but also for Chinese characters; the syntax format is " mb_substr($str,$start,$length,$encoding)".

How to implement Chinese string interception in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

mb_substr() function can select from a string Intercept the specified part. Different from the substr() function, the mb_substr() function is not only valid for English characters, but also for Chinese characters. Its syntax format is as follows:

mb_substr($str , $start [, $length = NULL [, $encoding = mb_internal_encoding()]])
Copy after login

The parameter description is as follows:

  • $str: The string to be intercepted, the string contains at least one character;
  • $start: The starting position of the intercepted string;
    • If $start is not If $start is a negative number, the string will be intercepted from the $start character of $str;
    • If $start is a negative number, the string will be intercepted from the end of $str to the $start character. Start intercepting the location.
  • $length: Optional parameter, indicating the length of the intercepted string;
    • If $length is a positive number, the string will be intercepted backwards from the $start position Up to $length characters;
    • If $length is a negative number, then $length characters at the end of $string will be omitted (if $start is a negative number, it will be counted from the end of the string);
    • If the value of $length is NULL or $length is omitted, it will be cut to the end of the string.
  • $encoding: Optional parameter, indicating the character encoding of $str. If omitted, the internal character encoding is used.

[Example] Use the mb_substr() function to intercept a Chinese string of specified length.

'; echo mb_substr($str, -19).'
'; echo mb_substr($str, 4, 6).'
'; echo mb_substr($str, 4, -16).'
'; echo mb_substr($str, -19, -13).'
'; echo mb_substr($str, -19, 6).'
'; var_dump(mb_substr($str, 40)); echo '
'; var_dump(mb_substr($str, 4, null)); ?>
Copy after login

The running results are as follows:

PHP中文网,一个在线学习编程的网站。 PHP中文网,一个在线学习编程的网站。 PHP中文网 PHP PHP中文网 PHP中文网 string(0) "" string(55) "PHP中文网,一个在线学习编程的网站。"
Copy after login

[Example] Use the mb_substr() function to intercept a string of specified length, and replace the excess part with "...".

18){ echo mb_substr($str, 0, 18).'...'; }else{ echo $str; } ?>
Copy after login

The running results are as follows:

PHP中文网是一个在线学习编程的网站...
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement Chinese string interception 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!