当前位置:网站首页>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”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道.
边栏推荐
猜你喜欢

【HMS core】【Media】【Video Editing Service】 The online material cannot be displayed, it is always in the loading state or the network is abnormal

经典实例分割模型Mask RCNN原理与测试

动态规划 --- 状态压缩DP 详细解释

(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
![[HMS core] [FAQ] Collection of typical problems of push kit, AR Engine, advertising service, scanning service 2](/img/08/9f2c7d1ea704f234c2a1882f85df24.png)
[HMS core] [FAQ] Collection of typical problems of push kit, AR Engine, advertising service, scanning service 2

Flask入门学习教程

70行代码撸一个桌面自动翻译神器

php如何截取字符串的前几位

【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常

FME realizes the method of converting CAD with attribute to SHP data
随机推荐
C# List<T> 模板的案例
TensorFlow custom training function
AL遮天传 DL-深度学习模型的训练技巧
TiDB 工具功能概览
flask获取post请求参数
Overview of TiDB Tool Functions
服务器装好系统的电脑怎么分区
L2-007 Family property (use of vector, set, map)
被捧上天的Scrum敏捷管理为何不受大厂欢迎了?
数据分析工具篇——HQL中DDL操作&DML操作
Store Limit usage documentation
tiup help
Golang分布式应用定时任务如何实现
Google engineer "code completion" tool; "Transformers NLP" accompanying book code; FastAPI development template; PyTorch model acceleration tool; cutting-edge papers | ShowMeAI News Daily
[Cloud Native] Service Industry Case - Solutions for Unpredictable Concurrency Scenarios
(Popular Science) What is Fractional NFT (Fractional NFT)
数组元素逆置
[HMS core] [FAQ] Collection of typical problems of push kit, AR Engine, advertising service, scanning service 2
TiUP FAQ
How to split microservices?