There are several mistakes. Don't use []
or strlen()
on a multibyte string. Use mb_substr()
and mb_strlen()
.
Also set the internal encoding of the multibyte extension to UTF-8
:
mb_internal_encoding("UTF-8");
You could also set the encoding on a per-function basis. See the specific function signatures for further details.
Here is an improved version of your code:
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding("UTF-8");
function randStr($rts = 20) {
$act_chars = 'ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ';
$act_val = '';
$act_chars_last = mb_strlen($act_chars);
for($act = 0; $act < $rts; $act++) {
$act_val .= mb_substr($act_chars, mt_rand(0, $act_chars_last), 1);
}
return $act_val;
}
$dene = randStr(16);
print $dene;
Replaced double quotes by single quotes (saves a tiny amount of time)
Removed mt_srand()
calls because they are automatically done as of PHP 4.2.0 (thanks to feeela for mentioning that in a comment)
Saved the string length - 1 in a variable, so PHP doesn't need to recomputed it in every loop step.
If you want more insights on that topic, checkout the link from deceze in the answer below: What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text.