a method of selecting random characters from given string

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

    Code:

    $string    = 'helloworld';
    $position  = rand(0, strlen($string));
    $character = $string[$position-1];
    
    echo $character . ' is placed ' . $position . ' in the following string: ' . $string;
    

    Output Example:

    e is placed 2 in the following string: helloworld


    CodePad Demo

提交回复
热议问题