Home > Article > Backend Development > How to get the first few characters of text in php
In PHP, you can use the "mb_substr" function to get the first few digits of a text. This function is used to return a part of a string. It is often used to split Chinese text. You can return the first few digits of a text string by setting parameters. bit, the syntax is "mb_substr("text",0,first few digits)".
The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer
The mb_substr() function returns a part of the string. We have learned the substr() function before. It only targets English characters. If you want to split Chinese characters, you need to use mb_substr().
Note: If the start parameter is negative and length is less than or equal to start, length is 0.
Syntax
mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] ) : string
The parameter settings are as follows:
The result returned is the extracted part of the string, and if it fails, it will be returned FALSE, or returns an empty string.
The example is as follows:
<?php echo mb_substr("这是需要截取的文字前几位这是不需要截取的文字后几位", 0, 12); // 输出:这是需要截取的文字前几位 ?>
Output result:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get the first few characters of text in php. For more information, please follow other related articles on the PHP Chinese website!