
Syntax :
It’s an inbuilt function of PHP. substr() function will return part of a string.
Returns the extracted part of a string, or FALSE on failure, or an empty string.
[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]
<?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:
<?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>
<?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>
<?php
var_dump(substr('ab', 3)); // bool(false)
?> Output of above code is :
bool(false)
When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…
In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…
In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…
In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…
When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…
In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…