PHP substr() Function


The substr() function returns a part of a string.

Syntax
substr(string,start,length)
Parameters
  • String - (Required) Specify the string to return a part.

  • start - (Required) Specify the where to start in string.
    Positive - start the specify position in string
    Negative - Start at a specify position from end of the string
    0 - start the first value of string.

  • length - (Required) Specify the length of a string.
    A positive number is the length to be returned from the start parameter
    A Negative number is the length to be returned from the end of the string
    If the length parameter is 0, NULL, or FALSE - it return an empty string
Example
<?php
    echo substr("Good Morning",5);
?>

Output

Morning.
Example
<?php    
    echo substr("Good Morning",5)."<br>";
    echo substr("Good Morning",-3)."<br>";
    echo substr("Good Morning",1)."<br>";
    echo substr("Good Morning",-5)."<br>";
?>

Output

Morning
ing
ood Morning
rning

Prev Next