note 110090 added to function.substr
| From: | RezaJahani::email at rezajahani dot com | Date: | Tue, 18 Sep 2012 11:25:05 +0000 |
| Subject: | note 110090 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190860@lists.php.net to get a copy of this message | ||
charAt for Persian/Arabic
Looking for a utf8-compatible PHP equivalent for JavaScript charAt(), I found that functions like
strlen(), substr(), etc. were giving me unexpected results. For example:
<?php
$str = 'داس';
echo(strlen($str), '; ', $str[2]);
?>
should apparently give "3, س" but none happens. So, I wrote this workaround:
<?php
function CharAt_Fa($str, $n) {
if ($n*2 <= strlen($str) && $n >= 0) {
$new_str = utf8_encode($str);
$charAt = substr($new_str, $n*4, 4);
return utf8_decode($charAt);
}
}
$string = 'داس';
echo CharAt_Fa($string, 0); //will produce "د"
?>
----
Server IP: 130.185.72.20
Probable Submitter: 109.125.178.75
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/110090
Del: integrated -- https://master.php.net/note/delete/110090/integrated
Del: useless -- https://master.php.net/note/delete/110090/useless
Del: bad code -- https://master.php.net/note/delete/110090/bad+code
Del: spam -- https://master.php.net/note/delete/110090/spam
Del: non-english -- https://master.php.net/note/delete/110090/non-english
Del: in docs -- https://master.php.net/note/delete/110090/in+docs
Del: other reasons-- https://master.php.net/note/delete/110090
Reject -- https://master.php.net/note/reject/110090
Search -- https://master.php.net/manage/user-notes.php