在PHP
的实战中,我们经常需要对URL
字符串进行解析,截取我们所需要的片段,PHP
为我们提供了substr()
函数,我们可很轻松的获取我们所需要的数据,本文就带大家一起来看一看PHP
中的substr()
函数。
substr ( string $string , int $start , int $length = ? )
代码实例:
1.若参数都为非负整数
<?PHP echo substr('PHP.cn is all right', 1); echo <br>; echo substr('PHP.cn is all right', 1, 3); echo <br>; echo substr('PHP.cn is all right', 0, 4); echo <br>; echo substr('PHP.cn is all right', 0, 8); ?>
输出: hp.cn is all right hp. PHP. PHP.cn i
2.若$start出现负整数
<?PHP echo substr(PHP.cn is all right, -1); echo <br>; echo substr(PHP.cn is all right, -2); echo <br>; echo substr(PHP.cn is all right, -3, 1); ?>
输出: t ht g
3.若$length出现负整数
<?PHP echo substr(PHP.cn is all right, 0,-1); echo <br>; echo substr(PHP.cn is all right,2, -1); echo <br>; echo substr(PHP.cn is all right,4, 4); echo <br>; echo substr(PHP.cn is all right,-3, -1); ?>
输出:PHP.cn is all righ p.cn is all righ cn i gh
推荐:《2021年PHP面试题大汇总(收藏)》《php视频教程》
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。