The Substr () PHP Function is used to return part of a string. It is written as substr (string, start, optional length);.The string is whatever string you want to return a portion of. The start is how many characters in to skip before starting. Setting this to 3 would skip the first three and start returning at the forth character. Setting this to a negative number will start counting backwards from the end of the string.
Length is an optional parameter. If you set this to a positive number, it will return that number of characters. If you set this to a negative number it will count that many numbers from the end of the string and return whatever is left in the middle.
Length is an optional parameter. If you set this to a positive number, it will return that number of characters. If you set this to a negative number it will count that many numbers from the end of the string and return whatever is left in the middle.
See how this works :
<?php
// this will return bcdefghijk
echo substr(‘abcdefghijk’, 1);
echo “<br>”;// this will return defghijk
echo substr(‘abcdefghijk’, 3);
echo “<br>”;// this will return abc
echo substr(‘abcdefghijk’, 0, 3);
echo “<br>”;// this will return ijk
echo substr(‘abcdefghijk’, -3);
echo “<br>”;// this will return bcdefghij
echo substr(‘abcdefghijk’, 1, -1);
echo “<br>”;// this will return cdefgh
echo substr(‘abcdefghijk’, 2, -3);
echo “<br>”;
?>
Related posts:
- PHP Tutorial – Part IV The WHILE Loop The WHILE loop is one of the most useful commands in PHP. It is also quite easy...
- Difference between isset empty is null I have come across code that checks for empty or null values, or if a variable is set. Many of...
- PHP Type Casting Contents Types Casting Cast to string Cast to int Cast to binary (PHP6) Cast to unicode (PHP6) Cast to boolean...
- PHP Tutorial – Part III The Basics Of IF If statements are used to compare two values and carry out different actions based on the...
- PHP Tutorial – Part VI Introduction In this part I will continue this and also show you how to use PHP and forms together to...












