note 110090 deleted from function.substr by googleguy
| From: | googleguy@php.net | Date: | Tue, 18 Sep 2012 18:36:20 +0000 |
| Subject: | note 110090 deleted from function.substr by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190868@lists.php.net to get a copy of this message | ||
Note Submitter: Reza Jahani :: email at rezajahani dot com
----
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 "د"
?>