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)
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…