note 75590 added to function.substr
| From: | Jeremy dot D at osu1 dot php dot net | Date: | Thu, 07 Jun 2007 06:48:15 +0000 |
| Subject: | note 75590 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127261@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 203.16.234.90
Probable Submitter: 203.153.244.198
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/75590
Del: integrated -- https://master.php.net/note/delete/75590/integrated
Del: useless -- https://master.php.net/note/delete/75590/useless
Del: bad code -- https://master.php.net/note/delete/75590/bad+code
Del: spam -- https://master.php.net/note/delete/75590/spam
Del: non-english -- https://master.php.net/note/delete/75590/non-english
Del: in docs -- https://master.php.net/note/delete/75590/in+docs
Del: other reasons-- https://master.php.net/note/delete/75590
Reject -- https://master.php.net/note/reject/75590
Search -- https://master.php.net/manage/user-notes.php