0

I need logic or some package with functionality to generate random string with specific encoding without using base function random_bytes etc.

Something like this:

$randomLine = random_string($length = 10, $encoding = 'UTF-16');
Kart Av1k
  • 137
  • 1
  • 6
  • What exactly is this going to be used for? – Andrei Sep 17 '18 at 12:24
  • Do you need the string need to be randomly spread over all the characters in the given encoding? Or could you generate a random string in a more simple encoding (say ASCII) and then convert it? Because the former will be considerably harder. Also *without using base function random_bytes etc* - why? Because again, that's going to be much more difficult. What *can* you use? – iainn Sep 17 '18 at 12:28
  • @iainn I need this specific functionality for hard tests of my service – Kart Av1k Sep 17 '18 at 13:55

1 Answers1

0

If you have php 7.2+, you can use mb_chr, like this

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

echo $out;

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

Chris Lear
  • 6,592
  • 1
  • 18
  • 26