Home  >  Article  >  Backend Development  >  How to intercept the first digits of Chinese string in php

How to intercept the first digits of Chinese string in php

藏色散人
藏色散人Original
2021-05-14 09:25:273170browse

php method to intercept the first few digits of a Chinese string: first create a PHP sample file; then intercept the first few digits of the Chinese string through the "mb_substr($str,0,4,'utf-8');" method Just a few people.

How to intercept the first digits of Chinese string in php

#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

How to intercept the first digits of a Chinese string in php?

Chinese interception: mb_substr()

mb_substr( $str, $start, $length, $encoding )

$str, the string that needs to be truncated

$start, the starting point of truncation, the starting point is 0

$length, the number of words to be intercepted

$encoding, web page encoding, such as utf-8, GB2312, GBK

Example:

The code is as follows:

<?php
$str=&#39;快乐老家:https://www.xx.net&#39;;
echo mb_substr($str,0,4,&#39;utf-8&#39;);//截取头5个字,假定此代码所在php文件的编码为utf-8
?>

The results show: Happy Hometown

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to intercept the first digits of Chinese string 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