substr
Return part of the string
substr()
function returns part of the string.
Note: If the start parameter is a negative number and length is less than or equal to start , then length is 0.
Return "world" from the string:
<?php echo substr ( "Hello world" , 6 ) ; ?>
Try it yourself
Use start parameters with different positive and negative numbers:
<?php echo substr ( "Hello world" , 10 ) . "<br>" ; echo substr ( "Hello world" , 1 ) . "<br>" ; echo substr ( "Hello world" , 3 ) . "<br>" ; echo substr ( "Hello world" , 7 ) . "<br>" ; echo substr ( "Hello world" , - 1 ) . "<br>" ; echo substr ( "Hello world" , - 10 ) . "<br>" ; echo substr ( "Hello world" , - 8 ) . "<br>" ; echo substr ( "Hello world" , - 4 ) . "<br>" ; ?>
Try it yourself
Use start and length parameters with different positive and negative numbers:
<?php echo substr ( "Hello world" , 0 , 10 ) . "<br>" ; echo substr ( "Hello world" , 1 , 8 ) . "<br>" ; echo substr ( "Hello world" , 0 , 5 ) . "<br>" ; echo substr ( "Hello world" , 6 , 6 ) . "<br>" ; echo substr ( "Hello world" , 0 , - 1 ) . "<br>" ; echo substr ( "Hello world" , - 10 , - 2 ) . "<br>" ; echo substr ( "Hello world" , 0 , - 6 ) . "<br>" ; echo substr ( "Hello world" , - 2 - 3 ) . "<br>" ; ?>
Try it yourself
parameter | describe |
---|---|
string | Required. Specifies that some of the strings are returned. |
start |
Required. Specifies where to start in the string.
|
length |
Optional. Specifies the length of the returned string. The default is until the end of the string.
|