Parameter Explanation and Usage Example of substr of Function in php

  • 2021-07-26 07:18:57
  • OfStack

In this paper, the parameter description and usage of substr () function in php are described with examples. Share it for your reference. The details are as follows:

string substr (string $string, int $start [, int $length]), which can be used to find a matching string or character in a longer string, $string is the string to be processed, $start is the starting selection position, and $length is the length to be selected.

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

Read characters right to left when $length is negative.

string Required, specifying that one part of the string is to be returned.

start Required, specifying where the string begins.

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

Positive number-starts at the specified position of the string

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

0-Begins at the first character in the string

The PHP example 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 is helpful to everyone's PHP programming.


Related articles: