note 22411 deleted from function.substr by aidan
| From: | aidan@php.net | Date: | Sat, 03 Jul 2004 07:54:26 +0000 |
| Subject: | note 22411 deleted from function.substr by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72388@lists.php.net to get a copy of this message | ||
Note Submitter: maxim@php.net
----
When you need a first element (or another one you know the position of) you can do this:
$str = 'Maxim Maletsky';
echo '<p>The first letter of my name is (' . $str[0] . ')';
// This prints:
// First letter of my name is (M)
//
// This example is the same as substr($str, 0, 1);
---
// Now, the same thing is when you need to find out the last letter:
echo '<p>The last letter of my name is (' . $str[strlen($str)-1] . ')';
// This prints:
// First letter of my name is (y)
//
// This example is the same as substr($str, -1);
---
This example can explain it better:
for($i=0; $i<strlen($str); $i++) {
echo "<br> Character [$i] is (" . $str[$i] . ")";
}
Using this instead of substring might help you a bit not to create too many confusion.
Maxim Maletsky
maxim@php.net