Joe Wilkinson
Joe Wilkinson

Reputation: 111

How to generate random name of person using php?

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.

Upvotes: 10

Views: 37942

Answers (3)

Warren
Warren

Reputation: 2014

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

Upvotes: 9

David Hempy
David Hempy

Reputation: 6237

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

Upvotes: 11

Leandro Papasidero
Leandro Papasidero

Reputation: 3738

<?php
function randomName() {
    $names = array(
        'Juan',
        'Luis',
        'Pedro',
        // and so on

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

print randomName();

Upvotes: 3

Related Questions