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

Thank 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

share|improve this question
    
rand() will give you random numbers and dot (.) performs string concatenation, e.g. echo 'john' . rand(1, 1000) . '.smith'; might yield john271.smith. – David Harkness Feb 15 '11 at 6:46
up vote 5 down vote accepted

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));
share|improve this answer
    
That's pretty good, but I think it'd be a random byte in this case, rather than a random bit. – muddybruin Feb 15 '11 at 6:48
    
Sorry, I meant it to be random parts, rather then bits or bytes. Couldn't think of the word but thanks for mentioning it, i've updated it – Jason Feb 15 '11 at 6:49
    
@Jason: Very Good Logic and implementation. Thank You. But i am getting an error at implode function Warning: implode() [function.implode]: Invalid arguments passed in – KillerFish Feb 15 '11 at 6:54
    
@Jason: I got it. we have to shuffle the array outside the implode function. shuffle($items); and then implode($delimiter, $items); – KillerFish Feb 15 '11 at 7:15
    
oh yeah, shuffle returns a bool, not the modified array. – muddybruin Feb 15 '11 at 7:25

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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.