note 81678 added to function.substr
| From: | yornatnodotspamatniroj dot com at osu1 dot php dot net | Date: | Sun, 09 Mar 2008 17:59:24 +0000 |
| Subject: | note 81678 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136872@lists.php.net to get a copy of this message | ||
kriskra's charAt script has the nifty feature that when $pos == -1, it will give the last
letter of the string.
I am not sure (did not test), but I believe this code might be slightly faster:
<?php
/**
* Function to get a single character from a string
*
* @param string $str String to get char from
* @param integer $pos Get char from this position of $str. Should be zero or greater
* @return mixed Integer -1 when $pos is out of range, otherwise the charater at position $pos.
*/
function charAt($str, $pos) {
return ($pos < 0 || $pos >= strlen($str)) ? -1 : $str{$pos};
}
?>
Please note that this version does NOT have this nifty feature.
When $pos == -1, result will be -1.
----
Server IP: 83.137.20.135
Probable Submitter: 2001:610:1908:8000:20d:93ff:fe79:c26e
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/81678
Del: integrated -- https://master.php.net/note/delete/81678/integrated
Del: useless -- https://master.php.net/note/delete/81678/useless
Del: bad code -- https://master.php.net/note/delete/81678/bad+code
Del: spam -- https://master.php.net/note/delete/81678/spam
Del: non-english -- https://master.php.net/note/delete/81678/non-english
Del: in docs -- https://master.php.net/note/delete/81678/in+docs
Del: other reasons-- https://master.php.net/note/delete/81678
Reject -- https://master.php.net/note/reject/81678
Search -- https://master.php.net/manage/user-notes.php