随机字符串编码

问题描述 投票:0回答:1

我需要逻辑或一些具有功能的包来生成具有特定编码的随机字符串,而不使用基函数random_bytes等。

像这样的东西:

$randomLine = random_string($length = 10, $encoding = 'UTF-16');
php string random encoding
1个回答
0
投票

如果你有php 7.2+,你可以像这样使用mb_chr

<?
$max = 10;
$out = '';
$encoding = 'UTF-16';
for ($i = 0;$i<$max;$i++) {
    if ($s = mb_chr(rand(1,1114112), $encoding)) {
        $out .= $s;
    }
}

echo $out;

http://sandbox.onlinephpfunctions.com/code/a4d39412005473231317758ca5d2fc8d4ad0b27b

© www.soinside.com 2019 - 2024. All rights reserved.