Home >Backend Development >PHP Tutorial >Detailed explanation of the usage of PHP substr() function
This article mainly introduces the usage of PHP stubstr() function. substr() is a built-in function in PHP, which is used to extract a part of a string, that is, return a substring of a string.
Syntax:
substr(string_name, start_position, string_length_to_cut)
Parameters:
The substr() function allows three parameters, two of which are mandatory and one is optional.
string_name: In this parameter we pass the original string or the string that needs to be cut or modified. This is a mandatory parameter.
start_position: If start_position is a non-negative number, the returned string will start from the start position of string and start counting from 0. If start_position is negative, the returned string will start at the start character from the end of string. This is also a mandatory parameter.
string_length_to_cut: This parameter is optional and is of integer type. This refers to the length of the portion of the string that needs to be cut from the original string. If the integer is positive, it refers to starting from start_position and extracting the length from the beginning. If the integer is negative, then it refers to starting from start_position and extracting the length from the end of the string. If this parameter is not passed, the substr() function will return the string starting from start_position until the end of the string.
Return type:
If successful, return the extracted string part; if failed, return FALSE or an empty string.
PHP substr() function usage example:
<?php function Substring($str){ $len = strlen($str); echo substr($str, 6), "<br>"; echo substr($str, 3, $len), "<br>"; echo substr($str, -11, 11), "<br>"; echo substr($str,-11, -8), "<br>"; } $str="phpAndmysql"; Substring($str);
Output:
mysql Andmysql phpAndmysql php
Related recommendations: "PHP Tutorial》
The above is the detailed content of Detailed explanation of the usage of PHP substr() function. For more information, please follow other related articles on the PHP Chinese website!