当前位置:网站首页>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”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道.
边栏推荐
猜你喜欢
timed task corn
arcpy使用教程
涨姿势了!原来这才是多线程正确实现方式
Local Transactions vs Distributed Transactions
华为ADS获取转化跟踪参数报错:getInstallReferrer IOException: getInstallReferrer not found installreferrer
70 lines of code, a desktop automatic translation artifact
Example of video switching playback (video switching example) code
【HMS core】【Media】【Video Editing Service】 The online material cannot be displayed, it is always in the loading state or the network is abnormal
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
[AGC] Quality Service 1 - Example of Crash Service
随机推荐
Extremely Knowing v2 Analysis
flask获取post请求参数
90后人大硕士为学医竟重新高考,成功被首医大录取
【HMS core】【FAQ】push kit、WisePlay DRM、Location Kit、Health Kit、3D Modeling Kit、SignPal Kit典型问题合集4
Google engineer "code completion" tool; "Transformers NLP" accompanying book code; FastAPI development template; PyTorch model acceleration tool; cutting-edge papers | ShowMeAI News Daily
Shell脚本的概念
100w的数据表比1000w的数据表查询更快吗?
几种常见的存储器
Compile, link, notes - 3
CAD几个优化设置
FME读写cass数据的方案及操作流程
数组元素逆置
Golang分布式应用定时任务如何实现
tiup env
数据分析工具篇——HQL中DDL操作&DML操作
xxl-job源码解析(技术分享)
数据库 - 创建数据库、表、函数等
应用OPC解决方案实现控制系统数据的安全交换
【HMS core】【FAQ】Account、IAP、Location Kit and HarmonyOS典型问题合集1
Back waves are coming!Ali produced the "second generation" container technical manual and brain map, which is too fragrant