I have the following code which is not working with UTF-8 characters. How can I fix it?
$seed = preg_split('//u', $seed, -1, PREG_SPLIT_NO_EMPTY);
$seed = str_split('АБВГДЕЖЗ'); // and any other characters
shuffle($seed); // probably optional since array_is randomized; this may be redundant
$code = '';
foreach (array_rand($seed, 5) as $k) $md5_hash .= $seed[$k];
//We don't need a 32 character long string so we trim it down to 5
$security_code = $code;
I have tried this code:
$seed = preg_split('//u', $seed, -1, PREG_SPLIT_NO_EMPTY);
but it is still not working.
For working with UTF-8 strings use Multibyte String Functions.
For your purpose it will be
mb_split
.Update
You must create the variable
$seed
and give it a string value before you can use it as the second parameter ofpreg_split
:The output of
print_r($seed)
will be:I hope the rest of your code will work just fine.