开发者

How to generate a random string in PHP (using 3 strings and random numbers)

I have three strings and i want to generate a random string using those 3 strings with including some random number between the strings.

T开发者_如何学JAVAhank You

Ex: first string : john
    second string : smith
    third string : john9k

    I want a random string like : john.simth190, smith.john9k, john9k.123.smith, etc.,

How to do this in PHP.

Thank You


You can try something like this:

<?php 
function random($items, $min=0, $max=100, $random_parts=2, $delimiter="."){
    #make sure items is an array
    $items = (array)$items;        

    #add as many random bits as required.
    for($x=0; $x<$random_parts; $x++)
          $items[] = rand($min, $max);

    #shuffle and join them
    shuffle($items);
    return implode($delimiter, $items);
}

Basically what it does is accept an array of the names, array('john','smith','john9k'). Then it takes the min rand and the max rand parameters. Finally it accepts the amount of random numbers you want.

So to call It i'd do this:

<?php
echo random(array('john','smith','john9k'), 0, 100, rand(0,10));


you can try this too

$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]].$input[$rand_keys[1]].rand(0,100);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜