Home  >  Article  >  Backend Development  >  How to get the first few characters of text in php

How to get the first few characters of text in php

WBOY
WBOYOriginal
2022-02-17 10:22:362366browse

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)".

How to get the first few characters of text in php

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

How to get the first few digits of text in php

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:

How to get the first few characters of text in php

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:

How to get the first few characters of text in php

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!

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