新闻资讯
看你所看,想你所想

php substr

php substr

php substr是一种基于php语言开发的可以返回字元串的一部分的功能函式。

基本介绍

  • 中文名:php substr
  • 适用领域範围:程式设计
  • 定义和用法: 函式返回字元串的一部分
  • 语法:substr(string,start,length)

语法

substr(string,start,length)
参数
描述
string
必需。规定要返回其中一部分的字元串。
start
必需。规定在字元串的何处开始。
正数 - 在字元串的指定位置开始

负数 - 在从字元串结尾的指定位置开始

0 - 在字元串中的第一个字元处开始

length
可选。规定要返回的字元串长度。默认是直到字元串的结尾。
正数 - 从 start 参数所在的位置返回

负数 - 从字元串末端返回

提示和注释

注释:如果 start 是负数且 length 小于等于 start,则 length 为 0。

例子 1

作用: 截取字元串Hello world! 后第7个字元开始的内容,
运行结果: world!
<?php
echo substr("Hello world!",6);
?>

例2

substr('php is very good language',4,5);
输出为 is ve;
当start>str的长度,则返回为();
substr('php is very good language',26,5);
substr('php is very good language',4);
输出为 (空白)
输出为is v (表明start和langth都为4)
当start为负值,则从str末尾出开始读起(*这时是从-1开始读,而不是从0开始),
substr('php is very good language',-4,5);
输出为uage
当length为负值时,length代表的是从末尾开始读,截取str的结束位置。
substr('php is very good language',4,-5);
输出为is very good lan

相关推荐

声明:此文信息来源于网络,登载此文只为提供信息参考,并不用于任何商业目的。如有侵权,请及时联系我们:yongganaa@126.com