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

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools