定义和用法

substr() 函数返回字符串的一部分

语法

substr(string,start,length)

参数解析

参数描述string必需。规定要返回其中一部分的字符串。start

必需。规定在字符串的何处开始。

正数 - 在字符串的指定位置开始

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

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

length

可选。规定被返回字符串的长度。默认是直到字符串的结尾。

正数 - 从start参数所在的位置返回的长度

负数 - 从字符串末端返回的长度

示例

<?phpechosubstr("Helloworld",10)."<br>";echosubstr("Helloworld",1)."<br>";echosubstr("Helloworld",3)."<br>";echosubstr("Helloworld",7)."<br>";echosubstr("Helloworld",0,10)."<br>";echosubstr("Helloworld",1,8)."<br>";echosubstr("Helloworld",0,5)."<br>";echosubstr("Helloworld",6,6)."<br>";?>

输出

delloworldloworldorldHelloworlelloworHelloworld