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

WBOY
Release: 2016-07-13 10:00:06
Original
706 people have browsed it

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

 这篇文章主要介绍了php字符串函数学习之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/975118.htmlTechArticlephp字符串函数学习之substr() 这篇文章主要介绍了php字符串函数学习之substr(),本文讲解了它的定义和用法、参数描述、提示和注释以及多个使...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!