a method of selecting random characters from given string

前端 未结 15 1970
小鲜肉
小鲜肉 2020-12-03 21:53

Can anyone help me with a solution that pulls the position and value of a random character from a given string using PHP. For example I have a a string variable $string = \

15条回答
  •  感情败类
    2020-12-03 22:26

    $string = 'helloworld';
    
    //generate a random position between 0 and string length ( note the -1 )
    $randPos = mt_rand(0, strlen($string)-1);
    
    echo 'Position : ' . $randPos . "\n";
    echo 'Character : ' . $string[$randPos] . "\n";
    

提交回复
热议问题