Substring - PHP
- 1 Comment
![]()
More about PHP substr() function >>>
You know substr()
function in php. The normal usage of this function is here:
Sytax: substr($str, $start, $length);
<?php echo substr(’hello
world’,7,3); ?>
// First parameter is the string, second is
the start index (first character of the string starts from 0) and third param
is the length of the substring
Output: orl .
If we not specified the length parameter (parameter 3),
returns the string from $start to the end of the string.
example:
<?php echo substr(’hello world’,7); ?>
gives the output: orld
If the $start parameter is a negative number, the position
starts from end of the string. Example:
<?php
echo substr(’on the way to operating theatre’, -9) ?>
gives an output: g theatre
another example:
<?php
echo substr(’on the way to operating theatre’, -9,3) ?>
output: g t
If $length is negative, substr( ) counts back from the end of the string to
determine
where your substring ends:
eg: <?php
echo substr(’on the way to operating theatre’, 4,-3) ?>
output: he way to operating thea
another eg: <?php
echo substr(’on the way to operating theatre’, -9,-3) ?>
output: g thea
Thanks and Regards
Syth




