Zend - The PHP Company




Text

Add Code


Random Name Generator  

Type: application
Added by: weeder
Entered: 29/09/2000
Last modified: 09/12/2002
Rating: - (fewer than 3 votes)
Views: 11612
As the title implies, this is a random name generator, ported to PHP by me, was originally in C.


/*********************************************************************
 * file       : namegen.php                                          *
 * version    : 0.1                                                  *
 * author     : dave (dave@spacemoose.com)                           *
 * works with : php v3+ (i think?)                                   *
 * desc       : this scriptlet returns # amount of random character  *
 *              names, based on the ruleset used.                    *
 *                                                                   *
 *              this was ported to PHP from Johan Danforth's name    *
 *              generator program, which is at:                      *
 *              http://spitfire.ausys.se/johan/names/default.htm     *
 *                                                                   *
 * notes      : you can find rulesets at:                            *
 *              http://spitfire.ausys.se/johan/names/rules.zip       *
 *                                                                   *
 *              some rulesets have all uppercase names, while others *
 *              are lowercase. if you plan to use these rulesets on  *
 *              a non-windows server, rename them to all lowercase   *
 *              first, or change the $filedesc array accordingly.    *
 *                                                                   *
 *********************************************************************/
  
  $default_amount = 50;  /* pretty self explanatory */
  $max_amount     = 100; /* ... */

  /* list of .nam ruleset files and their descriptions */
  $filedesc = array( "f_male"   => "Mixed Male Fantasy",
                     "f_female" => "Mixed Female Fantasy",
                     "deverry1" => "Male Deverry-style",
                     "deverry2" => "Female Deverry-style",
                     "kerrel"   => "Elven Deverry-style",
                     "alver1"   => "Elven Male Tolkien-style",
                     "alver2"   => "Elven Female Tolkien-style",
                     "dvargar"  => "Male Dwarven Tolkien-style",
                     "hober"    => "Male Halfling Tolkien-style",
                     "orc1"     => "Orc Tolkien-style",
                     "orc2"     => "Orc Warhammer-style",
                     "felana"   => "Mixed Felana-style"
                   );

  $amount = abs((int)$amount);
  if ($amount > $max_amount) { $amount = $max_amount; }
  if ($amount < 1) { $amount = $default_amount; }

?>

<html><head><title>random name generator</title></head><body>
<center>

<table>
<form action="<? echo $PHP_SELF?>" method="post">
<td>Type of names to generate:</td><td><select name="fname">
<?

  
while(list($key,$val) = each($filedesc)) {
    if (
$fname == $key) { $selected " selected"; }
    else { 
$selected ""; }
    echo 
'<option value="'.$key.'"'.$selected.'>'.$val."n";
  }

?>
</select>
</td></tr><tr><td>
Number of names to generate:</td><td><input name="amount" value="<? if ($amount) echo $amount; else echo "$default_amount"?>" maxlength="3" size="3">
</td></tr><tr><td>
<input type="checkbox" name="shownum" value="yes"<? if ($shownum == "yes") echo " checked"?>> Show numbers
</td><td><input type="submit" value="Generate">
</td></tr></table>
</form>
<p>
<?

  
function generate_random_name($fname ""$amount$shownum "") {

    
$fp = @file($fname,"r");
    if (!
is_array($fp)) { my_die("Ruleset not found."); }

    while (list(,
$val) = each($fp)) {

      
$val trim(chop($val));

      if (
ereg("/*(.*)*/"$val)) { continue; }

          if (
strcasecmp("[startstav]"$val) == 0) { $type "start";  continue; }
      elseif (
strcasecmp("[mittstav]",  $val) == 0) { $type "middle"; continue; }
      elseif (
strcasecmp("[slutstav]",  $val) == 0) { $type "end";    continue; }
      elseif (
strcasecmp("[stop]",      $val) == 0) { break; }

          if (
$type == "start")  $start[]  = $val;
      elseif (
$type == "middle"$middle[] = $val;
      elseif (
$type == "end")    $end[]    = $val;
    }

    
srand((double)microtime()*1000000);

    for(
$a 0$a $amount$a++) {

      if (
$start)  $random_name  =  $start[(rand() %  count($start))];
      if (
$middle$random_name .= $middle[(rand() % count($middle))];
      if (
$end)    $random_name .=    $end[(rand() %    count($end))];

      if (
$shownum == "yes") { $return .= ($a 1).": "; }
      
$return .= "$random_name<br>n";
    }

    return(
$return);
  }
  
  function 
my_footer() {
    echo 
'</center><p><table align=center border=0 bgcolor=#F6F6F6>';
    echo 
'<tr><td>Ported to PHP by
          <a href="mailto:dave@spacemoose.com">weeder</a>,
          originally by <a href="http://spitfire.ausys.se/johan/names/default.htm">Johan Danforth</a>
          </tr></td></table>'
;
  }

  function 
my_die($message "") {
    echo(
$message);
    
my_footer();
    die();
  }

  if (
$fname) {
    if (
$filedesc["$fname"] == "") { my_die("Ruleset not found."); }

    
$random_names generate_random_name("$fname.nam"$amount$shownum);
    if (
$random_names) {
      echo 
"You selected $amount ".$filedesc["$fname"]." names";
      if (
$shownum == "yes") { echo " with numbers shown"; }
      echo 
":<p>n";
      echo 
"$random_names";
    }
    else {
      echo 
"Found no matches.n";
    }
  }

  echo 
my_footer();

?>

</center></body></html>


Usage Example




Rate This Script





Search



This Category All Categories