Reputation: 185
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.
Upvotes: 16
Views: 24090
Reputation: 574
Since noone mentinoned any utf8 (multibyte) safe method, here is mine:
mb_internal_encoding("UTF-8");
$possible="aábcdeéfghiíjklmnoóöópqrstuúüűvwxyzAÁBCDEÉFGHIÍJKLMNOÓÖŐPQRSTUÚVWXYZ";
$char=mb_substr($possible,rand(0, mb_strlen($possible) - 1),1);
Upvotes: 4
Reputation: 21
$name = "Hello";
echo $name[rand(0,strlen(substr($name,0,strlen($name)-1)))];
Upvotes: 2
Reputation: 177
For anyone who might want to use it for the same case like mine:
I just wrote a simple SecretGenerator:
echo generateSecret(10);
function generateSecret($length) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&!^§\$ß";
$key = "";
for($i = 0; $i <= $length; $i++){
$key .= $chars[mt_rand(0, strlen($chars)-1)];
}
return $key;
}
Will return something like this: "6qß^0UoH7NP"
Of course, this is not a secure function, but for generating simple Hashes it's quite okay to use.
Upvotes: 0
Reputation: 43619
Using mt_rand()
,
You can get the index of a random character by:
$charIndex = mt_rand(0, strlen($string)-1);
$char = $string[$charIndex]; // or substr($string, $charIndex, 1)
Upvotes: 2
Reputation: 3847
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
Upvotes: 0
Reputation: 1190
More internal overhead, but easy to read:
$str = 'helloworld';
$randChar = $str[array_rand(str_split($str))];
Upvotes: 0
Reputation: 133577
Just for fun:
$chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
echo $string[array_rand($chars)];
Upvotes: 0
Reputation: 9703
$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";
Upvotes: 0
Reputation:
sorry i dont know syntax for php (java \m/)
Upvotes: 0
Reputation: 43235
$str = "helloworld";
$randIndex = rand(0,strlen($str));
echo $str[$randIndex]," is at $randIndex";
Upvotes: 0
Reputation: 455010
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];
Upvotes: 0
Reputation: 7448
First choose a random number < strlen($yourchain)-1;
Then substr($yourchain,$random,$random+1);
Upvotes: 0
Reputation: 157863
$string = 'helloworld';
$pos = rand(0,(strlen($string)-1));
echo $pos."th char is: ". $string[$pos];
Upvotes: 8