function load_male_firstnames($limit = 500) { $fp = fopen(get_filename('male_firstnames.txt'), 'r'); $count = 0; while (!feof($fp)) { if ($count > $limit - 1) { fclose($fp); return $count; } else { $line = trim(fgets($fp)); if (strlen($line) > 0) { $row = unpack("A14name/A7freq/A7cumulfreq/A6rank", trim($line)); $firstname = new FirstName(); $firstname->name = ucfirst(strtolower(trim($row['name']))); $firstname->rank = ucfirst(strtolower(trim($row['rank']))); $firstname->gender = 'M'; $firstname->save(); $count++; } } } fclose($fp); return $count; }
/** * Generate a First Name * * Uses US Census data to get 250 most popular names for both male and female * * @param string $gender Do you want a male or female name? (M/F). If null, selects a gender at random. */ public function getFirstName($gender = null) { if (empty($gender)) { $gender = $this->getGender(); } return FirstName::where('gender', $gender)->where('rank', '<=', 250)->orderByRaw(Database::random())->first()->name; }