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 = \
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);
First choose a random number < strlen($yourchain)-1;
Then substr($yourchain,$random,$random+1);
sorry i dont know syntax for php (java \m/)
$str = "helloworld";
$randIndex = rand(0,strlen($str));
echo $str[$randIndex]," is at $randIndex";
More internal overhead, but easy to read:
$str = 'helloworld';
$randChar = $str[array_rand(str_split($str))];
echo substr($string, rand(0, strlen($string)-1), 1);