substr – PHP String Functions
Syntax :
Description :
It’s an inbuilt function of PHP. substr() function will return part of a string.
Parameter :
- string – This is a Required parameter. It is input string must be of minimum one character long.
- start_position – This is a Required parameter. Tells from where to start the replacement in input string.
- +ve number – Start replacing at the position specified. for example, in the string ‘xyzabc’, the character at 0 position is ‘x’, the char at position 3 is ‘a’, and so on.
- -ve number – Start replacing at the specified position from the end of a string.
- 0 (zero) – Start replacing at the first character in string.
- length – This is an Optional parameter. It tells the length of the returned string. Default is same as the length of the string.
- +ve number – The length to be returned from the start parameter.
- -ve number – The length to be returned from the end of the string.
- 0 (zero) – If the length is Zero(0), FALSE or NULL, an empty string will be returned.
- Omitted – The substring from the start parameter till the end of string is returned.
Output :
Returns the extracted part of a string, or FALSE on failure, or an empty string.
ChangeLog :
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
PHP Version, Description
7.0.0, If string is equal to start characters long then an empty string will be returned. Prior to this version – FALSE was returned in this case.
5.2.2 , If the start parameter indicates the position of a negative truncation or beyond then false is returned.
5.2.6, Other versions get the string from start.
[/table]
Related articles : substr_replace(), wordwrap(), trim(), chop().
substr() – PHP Functions Example 1 :
<?php echo substr("Hello from tutorialmines.",11); ?>
In above example, We get the string “tutorialmines.” as output from the input string “Hello from tutorialmines.”. Output of above code in the browser is as below:
substr() – PHP Functions Example 2 : Using different negative and position values for start length.
<?php echo substr("Hello from tutorialmines.",11)."<br>\n"; echo substr("Hello from tutorialmines.",5)."<br>\n"; echo substr("Hello from tutorialmines.",23)."<br>\n"; echo substr("Hello from tutorialmines.",8)."<br>\n\n"; echo substr("Hello from tutorialmines.",-1)."<br>\n"; echo substr("Hello from tutorialmines",-15)."<br>\n"; echo substr("Hello from tutorialmines",-4)."<br>\n"; echo substr("Hello from tutorialmines",-20)."<br>\n"; ?>
Output of above code in the browser is as below:
tutorialmines.<br>
from tutorialmines.<br>
s.<br>
om tutorialmines.<br>
.<br>
m tutorialmines<br>
ines<br>
o from tutorialmines<br>
substr() – PHP Functions Example 3 : Using all possible parameters
<?php // PHP program to show // substr_compare() function echo substr("Hello from tutorialmines.",0,20)."<br>\n"; echo substr("Hello from tutorialmines.",5,15)."<br>\n"; echo substr("Hello from tutorialmines.",0,5)."<br>\n"; echo substr("Hello from tutorialmines.",10,10)."<br>\n\n"; echo substr("Hello from tutorialmines.",0,-1)."<br>\n"; echo substr("Hello from tutorialmines.",-15,-2)."<br>\n"; echo substr("Hello from tutorialmines.",0,-16)."<br>\n"; ?>
Output of above code is :
Hello from tutorialm<br>
from tutorialm<br>
Hello<br>
tutorialm<br>
Hello from tutorialmines<br>
tutorialmine<br>
Hello fro<br>
substr() – PHP Functions Example 4 : ERRORS and EXCEPTIONS – Returns FALSE on error.
<?php var_dump(substr('ab', 3)); // bool(false) ?>
Output of above code is :
bool(false)