Return to Snippet

Revision: 11700
at February 15, 2009 17:36 by luctus


Initial Code
<?php
function generarUsername($nombre, apellido_paterno, $apellido_materno, $max_size = 8)
{
  $tentativo = false;
  $i = 0;
  $n = substr($nombre, 0, 1);
  while($tentativo == false && $n != $nombre && $i<15)
  {
    $a = $apellido_paterno.substr($apellido_materno, 0, $i);
    $n = substr($nombre, 0, (strlen($a)  + strlen($n) > $max_size)?(strlen($n) + 1):1);
    $tentativo = substr($n.$a, 0, $max_size);
			
    if(self::getByUsername($tentativo)){
      //This function check if the tentative username is available 
      $tentativo = false;
    }
    $i++;
  }
  return $tentativo;
}
?>

Initial URL

                                

Initial Description
In my app, we have to auto-generate usernames for our users, here is the code I did.

Initial Title
auto generate username from name and last name

Initial Tags
php

Initial Language
PHP