How to get the first few characters of text in php

WBOY
Release: 2023-03-15 11:20:02
Original
2371 people have browsed it

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
Copy after login

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);
// 输出:这是需要截取的文字前几位
?>
Copy after login

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!

Related labels:
php
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