note 75590 deleted from function.substr by tularis
| From: | tularis@php.net | Date: | Mon, 31 Dec 2007 10:48:29 +0000 |
| Subject: | note 75590 deleted from function.substr by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134611@lists.php.net to get a copy of this message | ||
Note Submitter: Jeremy .D
----
Reguarding: felipe at spdata dot com dot 29-Nov-2005 10:48
--------------------------------------------------------
JavaScript charAt PHP equivalent
<?php
function charAt($str, $pos)
{
return (substr($str, $pos, 1)) ? substr($str, $pos, 1) : -1;
}
?>
--------------------------------------------------------
There is a bug with the above code, if the charAt(..) is 0 it will return -1, because the
conditional returns false.
Code below with minor fix works:
<?php
function charAt($str, $pos)
{
return (strlen(substr($str, $pos, 1))) ? substr($str, $pos, 1) : -1;
}
?>