note 93884 added to function.substr

From: Date: Mon, 05 Oct 2009 11:42:19 +0000
Subject: note 93884 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-161346@lists.php.net to get a copy of this message
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; } } ?> ---- Server IP: 62.237.7.3 Probable Submitter: 94.22.124.78 ---- Manual Page -- http://www.php.net/manual/en/function.substr.php Edit -- https://master.php.net/note/edit/93884 Del: integrated -- https://master.php.net/note/delete/93884/integrated Del: useless -- https://master.php.net/note/delete/93884/useless Del: bad code -- https://master.php.net/note/delete/93884/bad+code Del: spam -- https://master.php.net/note/delete/93884/spam Del: non-english -- https://master.php.net/note/delete/93884/non-english Del: in docs -- https://master.php.net/note/delete/93884/in+docs Del: other reasons-- https://master.php.net/note/delete/93884 Reject -- https://master.php.net/note/reject/93884 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#161346) next »