开发者

a method of selecting random characters from given string

Can anyone help me with a solution that pulls the position and value of a random chara开发者_JAVA百科cter 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.


$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];


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);


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)


$name = "Hello";
echo $name[rand(0,strlen(substr($name,0,strlen($name)-1)))];


First choose a random number < strlen($yourchain)-1;

Then substr($yourchain,$random,$random+1);


echo substr($string, rand(0, strlen($string)-1), 1);


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];


$str = "helloworld";
$randIndex = rand(0,strlen($str));
echo $str[$randIndex]," is at $randIndex";


  1. find the length of the string , ($length)
  2. use random function to generate random value within that range , ($a=Random.get($length))
  3. use substring function to get the value of the random position.($string.substring($a,1))

sorry i dont know syntax for php (java \m/)


$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";


Just for fun:

$chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
echo $string[array_rand($chars)];


More internal overhead, but easy to read:

$str = 'helloworld';

$randChar = $str[array_rand(str_split($str))];


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜