Home > Article > Backend Development > How to get the characters after the number in php
Two methods: 1. Use the substr() function to intercept English strings, the syntax is "substr(string, starting position, number of characters)"; 2. Use the mb_substr() function to intercept Chinese or Chinese English mixed string, syntax "mb_substr(string, starting position, number of characters, "character encoding")".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php provides two methods to extract the string Characters after the number:
substr()
mb_substr()
1. Use the substr() function
substr($string, $start [, $length])The parameter description is as follows:
N (because characters are counted from 0, N-1 1 =N).
<?php header('content-type:text/html;charset=utf-8'); $str="Hello world"; echo "原字符串:".$str."<br>"; echo "取第1位之后的一个字符:".substr($str,1,1)."<br>"; echo "取第2位之后的一个字符:".substr($str,2,1)."<br>"; echo "取第3位之后的2个字符:".substr($str,3,2)."<br>"; echo "取第4位之后的全部字符:".substr($str,4)."<br>"; ?>
2. Use the mb_substr() function
mb_substr($str , $start [, $length = NULL [, $encoding = mb_internal_encoding()]])mb_substr() is better than substr( ) function has an additional optional parameter $encoding: indicating the character encoding of $str. If omitted, the internal character encoding is used.
<?php header('content-type:text/html;charset=utf-8'); $str = 'php中文网是一个在线学习编程的网站'; echo "原字符串:".$str."<br>"; echo "取第1位之后的一个字符:".mb_substr($str,1,1,"utf-8")."<br>"; echo "取第2位之后的一个字符:".mb_substr($str,2,1,"utf-8")."<br>"; echo "取第3位之后的2个字符:".mb_substr($str,3,2,"utf-8")."<br>"; ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to get the characters after the number in php. For more information, please follow other related articles on the PHP Chinese website!