note 93884 deleted from function.substr by cmb
| From: | cmb@php.net | Date: | Sun, 27 Oct 2019 09:08:42 +0000 |
| Subject: | note 93884 deleted from function.substr by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212827@lists.php.net to get a copy of this message | ||
Note Submitter: kaj dot strom at kapsi dot fi
----
One thing to keep in mind when using string indexes and UTF-8 is that string indexes are NOT
multi-byte safe.
<?php
$string = 'äää1';
echo $string[3];
?>
Outputs:
¤
When it logically should output "1". This is not a bug, as PHP 'normal' string
functions are not intended to be multi-byte safe. This can be solved by using this function
<?php
/**
*
* @param string $string String to "search" from
* @param int $index Index of the letter we want.
* @return string The letter found on $index.
*/
function charAt($string, $index){
if($index < mb_strlen($string)){
return mb_substr($string, $index, 1);
}
else{
return -1;
}
}
?>