PHP substr is a built-in function of PHP used to extract the specified part of a string. Substr() is a function used to cut a part of a string starting from a specific position to a particular length. In cases where the trimmed string is empty or any failure occurs, substr() will return false. PHP version 4 and above supports the substr() function. A string is among the data types PHP supports; these string variables contain alphanumeric values. String methods can manipulate these strings; one among them is substr().
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Given below is the syntax mentioned:
substr(string_name, starting_position_of_string, string_length_to_cut);
Parameters:
Substr() allows 3 arguments or parameters in which 2 are mandatory, and the other is an optional parameter.
Take an input string and apply the substr() method to get the specified portion of the string. Let us see some of the examples:
PHP substr() with no specific length.
Code:
<!DOCTYPE html> <html> <body> <?php echo substr("This is the first example of a PHP substr()",13); ?> </body> </html>
Output:
PHP substr() with negative starting position.
Code:
<!DOCTYPE html> <html> <body> <?php echo substr("This is the first example of a PHP substr()",-15); ?> </body> </html>
Output:
PHP substr() when length is 0.
Code:
<!DOCTYPE html> <html> <body> <?php echo substr("eduCBA is an e-learning site with 100+ tutorials", 14, 0). "</br>"; ?> </body> </html>
PHP substr() with negative length, negative starting position.
Code:
<!DOCTYPE html> <html> <body> <?php echo substr('PHP substr() returns a part of string', 5, 19); echo '<br>'; echo substr('PHP substr() returns a part of string', -5, 3); echo '<br>'; echo substr('PHP substr() returns a part of string',-18, -2); ?> </body> </html>
Output:
Here, ‘echo substr(‘PHP substr() returns a part of a string,’ -5, 3);’, -5 refers to the start of the input from the end of the string. 3 refers to returned strings cutting length.
Code:
<!DOCTYPE html> <html> <body> <?php $sample_string="eduCBA is an e-learning site with 100+ tutorials"; echo $sample_string; echo '<br>'; echo substr($sample_string,-10); echo '<br>'; echo substr($sample_string,-4, 3); echo '<br>'; echo substr($sample_string,5,-4); ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php var_dump(substr('eduCBA is an e-learning site with 100+ tutorials', 100)); ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php echo substr("eduCBA is an e-learning site with 100+ tutorials", 5); echo '<br>'; echo substr("eduCBA is an e-learning site with 100+ tutorials", 17); echo '<br>'; echo substr("eduCBA is an e-learning site with 100+ tutorials", -7); echo '<br>'; echo substr("eduCBA is an e-learning site with 100+ tutorials", -30); echo '<br>'; var_dump(substr("eduCBA is an e-learning site with 100+ tutorials", 100)); echo '<br>'; echo substr("eduCBA is an e-learning site with 100+ tutorials", 0, 16); echo '<br>'; echo substr("eduCBA is an e-learning site with 100+ tutorials", 2, 5); echo '<br>'; echo substr("eduCBA is an e-learning site with 100+ tutorials", -10, 4); echo '<br>'; echo substr("eduCBA is an e-learning site with 100+ tutorials", 0, -10); echo '<br>'; echo substr("eduCBA is an e-learning site with 100+ tutorials", -9, -1); ?> </body> </html>
Output:
With this, we conclude ‘PHP substr.’ Introduced to what PHP substr is and its syntax. How do the Parameters work in the syntax? Listed out a good number of examples to work on. We have mentioned the starting point to extract the input string. A starting point can be positive or negative; a negative starting point is extracting the input string from the end. The optional parameter ‘length’ can also be positive or negative or nothing. If the input string length is less than the length specified or no length is specified, substr() returns Boolean false.
The above is the detailed content of PHP substr. For more information, please follow other related articles on the PHP Chinese website!