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 = 'helloworld'; and would like to randomly select a character from $string and echo the character and its position.
Using random. choice() The random. choice() function is used in the python string to generate the sequence of characters and digits that can repeat the string in any order.
char letters[] = "abcdefghijklmnopqrstuvwxyz"; char x = letters[rand() % 26];
$str = 'helloworld';
$randomChar = $str[rand(0, strlen($str)-1)];
CodePad.
$string = 'helloworld';
$pos = rand(0,(strlen($string)-1));
echo $pos."th char is: ". $string[$pos];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With