sfk sfk - 19 days ago 11
PHP Question

php get string each character

When I run the below code, I get

?
instead of first character which is
Ş
.

If i don't use any Turkish characters, I don't have any problem but when I use this characters which is
Ş,İ,Ö,Ç
I'm having problems.

$str = "Şef";
echo $str[0];


or

$str="Şef";
for($i = 0; $i < strlen($str); $i++)
echo $str[$i] . '<br>';


How can resolve this problem? The header charset and HTML charset is utf-8 and my texteditor charset is utf-8 too.

Answer

I suggest you to use Multibyte String Functions http://docs.php.net/manual/en/ref.mbstring.php

mb_internal_encoding("UTF-8");

$str="Şef"; 

$len = mb_strlen($str);

for($i = 0; $i < $len; $i++) {
    echo  mb_substr($str, $i, 1) . '<br>';
}
Comments