The substr() is a built-in function in PHP that is used to extract a part of string.
Syntax:
substr(string_name, start_position, string_length_to_cut)
Parameters:
The substr() function allows 3 parameters or arguments out of which two are mandatory and one is optional.
- string_name: In this parameter, we pass the original string or the string that needs to cut or modified. This is a mandatory parameter
- start_position: This refers to the position of the original string from where the part needs to be extracted. In this, we pass an integer. If the integer is positive it refers to the start of the position in the string from the beginning. If the integer is negative then it refers to the start of the position from the end of the string. This is also a mandatory parameter.
- string_length_to_cut: This parameter is optional and of integer type. This refers to the length of the part of the string that needs to be cut from the original string. If the integer is positive, it refers to start from start_position and extract length from the beginning. If the integer is negative then it refers to start from start_position and extract length from the end of the string. If this parameter is not passed, then the substr() function will return the string starting from start_position till the end of string.
Return Type:
Returns the extracted part of the string if successful otherwise FALSE or an empty string on failure.
Below is a program to illustrate working of substr() in PHP:
<?php // PHP program to illustrate substr() function Substring($str){ $len = strlen($str); echo substr($str, 8), "\n"; echo substr($str, 5, $len), "\n"; echo substr($str, -5, 10), "\n"; echo substr($str,-8, -5), "\n"; } // Driver Code $str="GeeksforGeeks"; Substring($str); ?> |
Output:
Geeks forGeeks Geeks for
Recommended Posts:
- Node.js | substr() function
- JavaScript String substr() Method
- Difference between substr() and substring() in JavaScript
- TypeScript | String substr() Method
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

