Home  >  Article  >  Backend Development  >  Substr() function parameter description and usage examples in php, substr parameter description_PHP tutorial

Substr() function parameter description and usage examples in php, substr parameter description_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:00840browse

Parameter description and usage examples of substr() function in php, substr parameter description

The example in this article describes the parameter description and usage of the substr() function in php. Share it with everyone for your reference. The details are as follows:

string substr(string $string, int $start [, int $length]), it can be used to find matching strings or characters in a longer string, $string is the string to be processed, $start is the position to start selecting, $length is the length to be selected.

$length reads characters from left to right for positive data.

When $length is a negative number, characters are read from right to left.

string is required and specifies a part of the string to be returned.

start is required and specifies where to start the string.

charlist is optional and specifies the length of the string to be returned. The default is until the end of the string.

Positive number - starts at the specified position in the string

Negative number - starts at the specified position from the end of the string

0 - Start

at the first character in the string

PHP example code is as follows:

Copy code The code is as follows:
$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"

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/912290.htmlTechArticleParameter description and usage examples of substr() function in php, substr parameter description This article describes the substr() in php with examples Function parameter description and usage. Share it with everyone for your reference. The details are as follows:...
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