Answer the question
In order to leave comments, you need to log in
Cyrillic script not working?
There is a script
<?php
function generatePassword($length = 8){
$chars = 'yutsu';
$numChars = strlen($chars);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= substr($chars, rand(1, $numChars) - 1, 1);
}
return $string;
}
$n = "" . generatePassword(8) . "\n";
echo iconv_mime_encode('blabla: ', $n, array('input-charset' => 'UTF-8', 'output-charset' => 'windows-1251'));
?>
Here's a script whose output doesn't work. There are no errors, but nothing is displayed (as if there is no echo)
BUT if you replace $chars with English characters (qwer ...) - everything will work fine.
What is the problem that it does not work with Cyrillic and how to make it work with ru characters?
Thanks in advance
Answer the question
In order to leave comments, you need to log in
See the doc for mb_strlen and mb_substr. Your problem is that Cyrillic in utf-8 has 2 bytes per character, while strlen and substr only work for 1-byte characters.
It might make sense to take a look here: php.net/manual/en/book.mbstring.php
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question