10

Thanks anyone in advance for answering/attempting to answer my question.

I'm currently using a php script to generate random string, but now I'd like to generate random name of a person instead of generating just a random string. My old code looked something like this:

<?php
function RandomString($length) {
    $keys = array_merge(range('a', 'z'), range('A', 'Z'));
    for($i=0; $i < $length; $i++) {
        $key .= $keys[array_rand($keys)];
    }
    return $key;
}

print RandomString(6);
?>

Thanks Again in Advance.

Joe Wilkinson
  • 111
  • 1
  • 1
  • 8

3 Answers3

10

Names are just the beginning! Check out Faker:

<?php
require_once '/path/to/Faker/src/autoload.php';

$faker = Faker\Factory::create();

echo $faker->name; 
echo $faker->phoneNumber;  
echo $faker->paragraph(2);

This might produce the following output...different every time:

 John Smith

 800-867-5309

 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus
 et magnis dis parturient montes, nascetur ridiculus mus. Donec quam
 felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla
 consequat massa quis enim. Donec pede justo, fringilla vel, aliquet
 nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a,
 venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.

 Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean
 vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat
 vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra
 quis, feugiat a, tellus. 

More info here: https://github.com/fzaninotto/Faker

David Hempy
  • 5,373
  • 2
  • 40
  • 68
  • 1
    Thanks @David Hempy , although its a little complicated but, I'll give it a try in my script and see if it works out for me, but anyways thanks for the Good Reply! – Joe Wilkinson Feb 24 '15 at 06:50
  • Don't be afraid of it! It really is just the two lines of code shown here, then you can drop in fake names, addresses, salutations, anywhere you want. Give it a try! – David Hempy Jan 14 '20 at 16:46
8

I found this question because I needed the same thing. For anyone else looking, this is a function for 50 first names paired with 50 surnames giving 2500 possibilities. It's based on the accepted answer and the the random names are courtesy of http://listofrandomnames.com

echo randomName();

function randomName() {
    $firstname = array(
        'Johnathon',
        'Anthony',
        'Erasmo',
        'Raleigh',
        'Nancie',
        'Tama',
        'Camellia',
        'Augustine',
        'Christeen',
        'Luz',
        'Diego',
        'Lyndia',
        'Thomas',
        'Georgianna',
        'Leigha',
        'Alejandro',
        'Marquis',
        'Joan',
        'Stephania',
        'Elroy',
        'Zonia',
        'Buffy',
        'Sharie',
        'Blythe',
        'Gaylene',
        'Elida',
        'Randy',
        'Margarete',
        'Margarett',
        'Dion',
        'Tomi',
        'Arden',
        'Clora',
        'Laine',
        'Becki',
        'Margherita',
        'Bong',
        'Jeanice',
        'Qiana',
        'Lawanda',
        'Rebecka',
        'Maribel',
        'Tami',
        'Yuri',
        'Michele',
        'Rubi',
        'Larisa',
        'Lloyd',
        'Tyisha',
        'Samatha',
    );

    $lastname = array(
        'Mischke',
        'Serna',
        'Pingree',
        'Mcnaught',
        'Pepper',
        'Schildgen',
        'Mongold',
        'Wrona',
        'Geddes',
        'Lanz',
        'Fetzer',
        'Schroeder',
        'Block',
        'Mayoral',
        'Fleishman',
        'Roberie',
        'Latson',
        'Lupo',
        'Motsinger',
        'Drews',
        'Coby',
        'Redner',
        'Culton',
        'Howe',
        'Stoval',
        'Michaud',
        'Mote',
        'Menjivar',
        'Wiers',
        'Paris',
        'Grisby',
        'Noren',
        'Damron',
        'Kazmierczak',
        'Haslett',
        'Guillemette',
        'Buresh',
        'Center',
        'Kucera',
        'Catt',
        'Badon',
        'Grumbles',
        'Antes',
        'Byron',
        'Volkman',
        'Klemp',
        'Pekar',
        'Pecora',
        'Schewe',
        'Ramage',
    );

    $name = $firstname[rand ( 0 , count($firstname) -1)];
    $name .= ' ';
    $name .= $lastname[rand ( 0 , count($lastname) -1)];

    return $name;
}
Warren
  • 1,984
  • 3
  • 29
  • 60
3
<?php
function randomName() {
    $names = array(
        'Juan',
        'Luis',
        'Pedro',
        // and so on

    );
    return $names[rand ( 0 , count($names) -1)];
}

print randomName();
Leandro Papasidero
  • 3,728
  • 1
  • 18
  • 33
  • Thanks Lea Tano! I was looking for something like this. Can this array of names be extracted from an online source or maybe offline. Thanks for the reply anyways. – Joe Wilkinson Feb 24 '15 at 06:48