php中substr()函数参数说明及用法实例_php技巧

WBOY
Release: 2016-05-16 20:31:46
Original
1059 people have browsed it

本文实例讲述了php中substr()函数参数说明及用法。分享给大家供大家参考。具体如下:

string substr(string $string ,int $start [, int $length ]),它可以用于在一个较长的字符串中查找匹配的字符串或字符,$string为所要处理的字符串,$start为开始选取的位置,$length为要选取的长度.

$length 为正数据从左向右读取字符.

$length 为负数时就右向左读取字符.

string 必需,规定要返回其中一部分的字符串.

start 必需,规定在字符串的何处开始.

charlist 可选,规定要返回的字符串长度,默认是直到字符串的结尾.

正数 - 在字符串的指定位置开始

负数 - 在从字符串结尾的指定位置开始

0 - 在字符串中的第一个字符处开始

PHP实例代码如下:

复制代码代码如下:
$rest_1 = substr("abcdef", 2); // returns "cdef"
$rest_2 = substr("abcdef", -2); // returns "ef"

$rest1 = substr("abcdef", 0, 0); // returns ""
$rest2 = substr("abcdef", 0, 2); // returns "ab"
$rest3 = substr("abcdef", 0, -1); // returns "abcde"
$rest4 = substr("abcdef", 2,0); // returns ""
$rest5 = substr("abcdef", 2,2); // returns "cd"
$rest6 = substr("abcdef", 2, -1); // returns "cde"
$rest7 = substr("abcdef", -2,0); // returns ""
$rest8 = substr("abcdef", -2,2); // returns "ef"
$rest9 = substr("abcdef", -2,-1); // returns "e"

希望本文所述对大家的PHP程序设计有所帮助。

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
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!