Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate random name of person using php?

Tags:

html

php

random

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.

like image 730
Joe Wilkinson Avatar asked Feb 24 '15 04:02

Joe Wilkinson


People also ask

How to Generate random names in php?

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


2 Answers

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

like image 73
David Hempy Avatar answered Oct 02 '22 05:10

David Hempy


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;
}
like image 33
Warren Avatar answered Oct 02 '22 03:10

Warren