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

ISELED---the new choice of ambient lighting scheme

Delayed message queue

Extremely Knowing v2 Analysis

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

Back waves are coming!Ali produced the "second generation" container technical manual and brain map, which is too fragrant

几种常见的存储器

RISC-V调用惯例

Why is there no data reported when the application is connected to Huawei Analytics in the application debugging mode?

【AGC】质量服务1-崩溃服务示例

应用接入华为分析在应用调试模式下为何没有数据上报?
随机推荐
Promise笔记(一)
Introduction to kasini3000
TiUP terms and core concepts
Packages - Notes
针对 MySQL/InnoDB 刷盘调优
Store Limit usage documentation
flask获取post请求参数
Nature Microbiology综述:聚焦藻际--浮游植物和细菌互作的生态界面
路遇又一个流量风口,民宿长期向好的逻辑选对了吗
【HMS core】【FAQ】push kit、AR Engine、广告服务、扫描服务典型问题合集2
481-82 (105, 24, 82, 34, 153),
【AGC】质量服务1-崩溃服务示例
JHM:芳环羟化双加氧酶数据库DARHD建立及相关引物评价
CAD几个优化设置
影像信息提取DEM
Applicable scenarios of TiDB tools
Is Redis really slow?
在树莓派上驱动CSI摄像头
让人上瘾的新一代开发神器,彻底告别Controller、Service、Dao等方法
The Prospects of the Metaverse and the Four Tracks