Turkish Character issue in PHP and MySQL -
i'm trying count occurences of letters in turkish alphabet in mysql database.
when try count letter "a" this, correct result :
while($nt=mysql_fetch_array($rt)) { $mystring = $nt["word"]; for($i = 0; $i < strlen($mystring) ; $i++) { if($mystring[$i] == 'a') { $a++; } } }
when replace "a", "ç" zero. added code :
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("database unavailable"); mysql_set_charset('utf8', $bd);
how can fix code turkish characters? thanks.
in utf-8 ç
encoded 2 bytes (c3 a7
), therefore byte-by-byte comparison won't work. consider substr_count
:
$s = "abçdeç"; print substr_count($s, 'ç'); // 2
or use unicode-aware function this:
function utf8_char_count($s) { $count = []; preg_match_all('~.~u', $s, $m); foreach($m[0] $c) $count[$c] = isset($count[$c]) ? $count[$c] + 1 : 1; return $count; } print_r(utf8_char_count('çaüθç')); // [ç] => 2 [a] => 1 [ü] => 1 [θ] => 1
this assumes string utf-8, if not case (hint: var_dump(rawurlencode($str))
), check db , connection settings (see linked thread).
Comments
Post a Comment