SoFunction
Updated on 2025-03-09

Detailed explanation of the usage of substr function for PHP intercepting strings

substr() can "intercept" strings.

grammar

string substr( $str, start, length);

parameter

  • $str : The intercepted string.
  • start : The position to start intercepting.
  • length : The intercepted length.

Return value

  • The intercepted string is returned.
  • start: If the string length exceeds the string, return false
  • Set start  and length  to an unreasonable intercepting range, and return an empty string

1. Cut strings

1) The "index" of a string starts from 0, and "space" is also considered a character.

Example 1: Start from the 6th character on the left and intercept 4 characters to the right.

echo substr('0123456789',6,4).PHP_EOL;
echo substr('01234 6789',6,4);

Output:

6789
6789

2) start  and length  can be negative numbers, and negative numbers mean the opposite.

Example 2: Start from the 6th character on the right and cut 4 characters to the right.

echo substr('9876543210',-6,4);

Output:

5432

Example 3: Starting from the 6th character on the left, the third character on the right is intercepted.

echo substr('0123456789321',6,-3);

Output:

67893

Example 4: Starting from the 6th character on the right, the 4th character on the right is intercepted.

echo substr('9876543210',-6,-4);

Output:

54

2. Intercept Chinese strings

The character "English" accounts for 1 byte, the "Chinese" encoded by UTF8 accounts for 3 bytes, and the Chinese encoded by GB2321 accounts for 2 bytes.

When intercepting Chinese, a (UTF8 encoding) Chinese should be intercepted as 3 strings. The value of start and length is "multiple of 3", otherwise garbled code will be intercepted.

Example:

echo substr('Zero one, two, three, four, five, seven, eight, nine',0,3).PHP_EOL;
echo substr('Zero one, two, three, four, five, seven, eight, nine',3,3).PHP_EOL;
echo substr('Zero one, two, three, four, five, seven, eight, nine',3,1);

Output:

zero
one

3. Special value of length

1) length can be omitted, and the string "end" can be intercepted by default.

Example:

echo substr('0123',1).PHP_EOL;
echo substr('0123',-1);

Output:

123
3

2) When length is 0, null, and false, it means that 0 characters are intercepted, and substr() will return an empty string.

Example:

var_dump(substr('0123',1,0));
var_dump(substr('0123',1,null));
var_dump(substr('0123',1,false));

Output:

string(0) ""
string(0) ""
string(0) ""

4. The special value of start

1) Start When the string "length" exceeds the string, false will be returned.

Example:

var_dump(substr('0123',11));

Output:

bool(false)

2) start can be a numerical string, but the content must be a "pure number", otherwise an error will be reported.

Example:

var_dump(substr('0123','0'));

Output:

string(4) "0123"

3) start can be a "boolean type" (true=1; false=0).

Example:

var_dump(substr('0123',true));
var_dump(substr('0123',false));

Output:

string(3) "123"
string(4) "0123"

4) When start, when the operator contains, it will first "operate" and then "assign" the operation result to start.

Example:

var_dump(substr('0123',1+1));
var_dump(substr('0123',1*2));
var_dump(substr('0123',~~2));
var_dump(substr('0123',1&1));

Output:

string(2) "23"
string(2) "23"
string(2) "23"
string(3) "123"

5) When start is hexadecimal, it will be automatically converted to 0.

Example: 11's hexadecimal is 0xB

var_dump(substr('0123456789abcde',0xB));

Output:

string(4) "bcde"

This is the end of this article about the detailed explanation of the usage of PHP intercepting string substr() function. For more related content of PHP substr() function, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!