H
H
hamburger222015-10-12 22:33:07
PHP
hamburger22, 2015-10-12 22:33:07

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

3 answer(s)
A
Andrew, 2015-10-12
@hamburger22

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.

A
Alexey Skobkin, 2015-10-12
@skobkin

It might make sense to take a look here: php.net/manual/en/book.mbstring.php

S
Sergey Zhukov, 2015-10-12
@Adobe

mb_strlen

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question