Home >>PHP String Functions >PHP substr() Function
PHP substr() function is used to extract a part of a string. It returns a part of the given input string of certain length, specified by the start and length parameter.
Syntax:
substr($string, $start, $length);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter defines the input string to return a part of. |
start | This is a required parameter. This parameter defines where to start in the string. |
length | This is an optional parameter. This parameter defines the length of the returned string. By default it is end of the string. |
Here is an example of substr() function in PHP:
<html> <body> <?php echo substr("Hey Guys!! I am Jerry..",10); ?> </body> </html>
Here is an another example of substr() function in PHP:
<html> <body> <?php $str = "PHP Python PHPTOINT"; echo "Input String: ".$str; echo "<br><br>"; // Positive numbers: echo substr($str,0,10)."<br>"; echo substr($str,3,8)."<br>"; echo substr($str,0,5)."<br>"; echo substr($str,5,6)."<br>"; echo "<br>"; // Negative numbers: echo substr($str,0,-3)."<br>"; echo substr($str,-5,-2)."<br>"; echo substr($str,0,-6)."<br>"; ?> </body> </html>