Home  >  Article  >  Backend Development  >  php字符串函数学习之substr(),字符串substr_PHP教程

php字符串函数学习之substr(),字符串substr_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:00:08843browse

php字符串函数学习之substr(),字符串substr

/*
定义和用法
substr() 函数返回提取的子字符串, 或者在失败时返回 FALSE。

语法
substr(string,start,length)

参数 描述
string 必需。规定要返回其中一部分的字符串。
start 
必需。规定在字符串的何处开始。
非负数 - 从 string 的 start 位置开始,从 0 开始计算.
负数 - 从 string 结尾处向前数第 start 个字符开始。
如果 string 的长度小于或等于 start,将返回 FALSE。

length 
可选。规定要返回的字符串长度。默认是直到字符串的结尾。
正数 - 从 start 处开始最多包括 length 个字符(取决于 string 的长度)。
负数 - 去除从string 结尾处向前length个字符
如果提供了值为 0,FALSE 或 NULL 的 length,那么将返回一个空字符串。
*/

$str = "abcdefghijklmn";

$rest = substr($str, 0);  // 返回 "abcdefghijklmn"
echo $rest . "
";

$rest = substr($str, 1, 3);  // 返回 "bcd"
echo $rest . "
";

$rest = substr($str, -3);  // 返回 "lmn"
echo $rest . "
";

$rest = substr($str, -3, 2);  // 返回 "lm"
echo $rest . "
";

$rest = substr($str, 1, -3);  // 返回 "bcdefghijk"
echo $rest . "
";

$rest = substr($str, -7, -3);  // 返回 "hijk"
echo $rest . "
";
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/974672.htmlTechArticlephp字符串函数学习之substr(),字符串substr php /* 定义和用法 substr() 函数返回提取的子字符串, 或者在失败时返回 FALSE。 语法 substr(string,st...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn