Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a method of selecting random characters from given string

Tags:

php

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.

like image 512
kay Avatar asked Mar 15 '11 11:03

kay


People also ask

How do you generate a random character from a string in Python?

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.

How do you get random letters in C++?

char letters[] = "abcdefghijklmnopqrstuvwxyz"; char x = letters[rand() % 26];


2 Answers

$str = 'helloworld';

$randomChar = $str[rand(0, strlen($str)-1)];

CodePad.

like image 80
alex Avatar answered Sep 24 '22 08:09

alex


$string = 'helloworld'; 
$pos = rand(0,(strlen($string)-1));
echo $pos."th char is: ". $string[$pos];
like image 39
Your Common Sense Avatar answered Sep 22 '22 08:09

Your Common Sense