a method of selecting random characters from given string

前端 未结 15 1972
小鲜肉
小鲜肉 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:28

    You can use the rand($x,$y) function which returns a random number between $x and $y inclusive:

    $str = 'helloworld';
    
    // get a random index between 0 and the last valid index
    $rand_pos = rand(0,strlen($str)-1);
    
    // access the char at the random index.
    $rand_chr = $str[$rand_pos];
    

提交回复
热议问题