首页 > PHP教程 > PHP常用函数手册 > PHP String 函数

PHP substr() 函数

 函数原型

/**

 * 返回字符串的一部分

 * @link http://php.net/manual/en/function.substr.php

 * @param string $string  输入的字符串,必须至少有一个字符

 * @param int $start  如果 start 是非负数,返回的字符串将从 string 的 start 位置开始,从 0 开始计算。例如,在字符串 “abcdef” 

   中,在位置 0 的字符是 “a”,位置 2 的字符串是 “c” 等等。

   如果 start 是负数,返回的字符串将从 string 结尾处向前数第 start 个字符开始。如果 string 的长度小于 start,将返回 FALSE。

 * @param int $length [optional]  要返回的字符串长度。默认是直到字符串的结尾

 * @return string  返回提取的子字符串,失败返回 FALSE

 * @since 4.0

 * @since 5.0

 */

function substr ($string, $start, $length = null) {}

 

函数示例

<?php

echo substr("Hello world",6);  //输出 world

echo substr("Hello world",-6);  //输出 world

echo substr("Hello world",0,5);  //输出 Hello

echo substr("Hello world",6,5);  //输出 world

echo substr("Hello world",0,-6);  //输出 Hello

echo substr("Hello world",-5);  //输出 world

echo substr("Hello world",-10,-2);  //输出ello wor

echo substr("Hello",-1);  //输出 o

?>

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。