当前位置:网站首页>How to intercept the first few digits of a string in php
How to intercept the first few digits of a string in php
2022-07-30 15:54:00 【Million speed cloud】
phpHow to intercept the first few digits of a string
本文小编为大家详细介绍“phpHow to intercept the first few digits of a string”,内容详细,步骤清晰,细节处理妥当,希望这篇“phpHow to intercept the first few digits of a string”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧.
Two interception methods:1、使用substr()函数,只需要将该函数的第二个参数设置为0,第三个参数设置为NYou can intercept the front of the English stringN位,语法“substr(字符串,0,N)”.2、使用mb_substr()函数,只需要将该函数的第二个参数设置为0,第三个参数设置为Nbefore the string can be interceptedN位,语法“mb_substr(字符串,0,N,字符编码)”.
本教程操作环境:windows7系统、PHP8.1版、DELL G3电脑
PHPwant to intercept the string in,Two functions can be used:substr()函数或mb_substr()函数.
方法1:使用substr()函数截取字符串
substr() 函数可以从字符串的指定位置截取一定长度的字符.
substr(string,start,length)
参数 | 描述 |
---|---|
string | 必需.规定要返回其中一部分的字符串. |
start | 必需.规定在字符串的何处开始.
|
length | 可选.规定要返回的字符串长度.默认是直到字符串的结尾.
|
只需要将该函数的第二个参数设置为0,第三个参数设置为Nbefore the string can be interceptedN位.
<?phpheader('content-type:text/html;charset=utf-8'); $str="Hello world";echo "原字符串:".$str."<br>";echo "截取前1位:".substr($str,0,1)."<br>";echo "截取前2位:".substr($str,0,2)."<br>";echo "截取前3位:".substr($str,0,3)."<br>";echo "截取前4位:".substr($str,0,4)."<br>";echo "截取前5位:".substr($str,0,5)."<br>";?>
注:substr() There is no problem with the function's processing of English,But in development we often deal with more Chinese characters,substr() Although the function can also handle Chinese,但是不是很完美.
<?phpheader('content-type:text/html;charset=utf-8'); $str="欢迎来到亿速云";echo "原字符串:".$str."<br>";echo "截取前1位:".substr($str,0,1)."<br>";echo "截取前2位:".substr($str,0,2)."<br>";?>
So how to intercept Chinese characters??可以使用mb_substr()函数.
方法2:使用mb_substr()函数截取字符串
mb_substr() 函数可以从一个字符串中截取指定的一部分,与 substr() 函数不同的是,mb_substr() 函数不仅对英文字符有效,对中文字符同样有效.
mb_substr(string,start,length,encoding)
参数 | 描述 |
---|---|
str | 必需.从该 string 中提取子字符串. |
start | 必需.规定在字符串的何处开始.
|
length | 可选.规定要返回的字符串长度.默认是直到字符串的结尾.
|
encoding | 可选.字符编码.如果省略,则使用内部字符编码. |
只需要将该函数的第二个参数设置为0,第三个参数设置为Nbefore the string can be interceptedN位.
<?phpheader('content-type:text/html;charset=utf-8'); $str="欢迎来到亿速云";echo "原字符串:".$str."<br>";echo "截取前1位:".mb_substr($str,0,1,"utf-8")."<br>";echo "截取前2位:".mb_substr($str,0,2,"utf-8")."<br>";?>
<?phpheader('content-type:text/html;charset=utf-8'); $str="Hello world";echo "原字符串:".$str."<br>";echo "截取前1位:".mb_substr($str,0,1,"utf-8")."<br>";echo "截取前2位:".mb_substr($str,0,2,"utf-8")."<br>";?>
读到这里,这篇“phpHow to intercept the first few digits of a string”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道.
边栏推荐
猜你喜欢
AI遮天传 DL-CNN
php如何去除字符串最后一位字符
php如何截取字符串的前几位
Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer
【HMS core】【FAQ】push kit、WisePlay DRM、Location Kit、Health Kit、3D Modeling Kit、SignPal Kit典型问题合集4
FME realizes the method of converting CAD with attribute to SHP data
涨姿势了!原来这才是多线程正确实现方式
timed task corn
RobotStudio实现喷漆、打磨等功能(曲面路径生成与仿真)
Example of video switching playback (video switching example) code
随机推荐
Flask之路由(app.route)详解
tiup env
Recent learning defragmentation (24)
Image information extraction DEM
When the vite multi-page application refreshes the page, it will not be in the current route and will return to the root route
代码随想录笔记_哈希_1l两数之和
Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer
FME realizes the method of converting CAD with attribute to SHP data
RISC-V calling conventions
tiup help
Compile, link, notes - 3
tiup list
Core Topics under Microservice Architecture (2): Design Principles and Core Topics of Microservice Architecture
Load Base Split usage documentation
【HMS core】【FAQ】push kit、AR Engine、广告服务、扫描服务典型问题合集2
TiUP 故障排查
Store Limit usage documentation
难道Redis真的变慢了吗?
Why is there no data reported when the application is connected to Huawei Analytics in the application debugging mode?
Flask入门学习教程