search
Homephp教程php手册php字符串函数学习之substr()

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 . "
";
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment