note 109852 deleted from function.trim by peehaa
| From: | peehaa@php.net | Date: | Tue, 21 Mar 2017 08:42:24 +0000 |
| Subject: | note 109852 deleted from function.trim by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208929@lists.php.net to get a copy of this message | ||
Note Submitter: seyednaser at gmail dot com
----
To show off the empty positions in a string by means of trim():
<?php
$string = " Hello World! ";
echo $string;
echo " Has : ".strlen($string)." letter(s). One by one according to the
following:<br />";
echo "<br />".$rightt = strlen(ltrim($string)) - strlen(trim($string))." empty
position(s) from right.";
echo "<br />".$leftt = strlen(rtrim($string)) - strlen(trim($string))." empty
position(s) from left.<br />";
$length = strlen($string);
for($x = 0; $x < $length; $x++){
$letter = substr($string, $x, 1);
if($letter <> " ")
echo "<br />Position $x ===>
".substr($string, $x, 1);
else
echo "<br />Position $x ===> Empty";
}
?>
the output is:
Hello World! Has : 19 letter(s). One by one according to the following:
3 empty position(s) from right.
4 empty position(s) from left.
Position 0 ===> Empty
Position 1 ===> Empty
Position 2 ===> Empty
Position 3 ===> Empty
Position 4 ===> H
Position 5 ===> e
Position 6 ===> l
Position 7 ===> l
Position 8 ===> o
Position 9 ===> Empty
Position 10 ===> W
Position 11 ===> o
Position 12 ===> r
Position 13 ===> l
Position 14 ===> d
Position 15 ===> !
Position 16 ===> Empty
Position 17 ===> Empty
Position 18 ===> Empty