28

On my PHP site, currently users login with an email address and a password. I would like to add a username as well, this username they g\set will be unique and they cannot change it. I am wondering how I can make this name have no spaces in it and work in a URL so I can use there username to link to there profiles and other stuff. If there is a space in there username then it should add an underscore jason_davis. I am not sure the best way to do this?

5
  • 2
    There are plenty questions like this. Didn’t you get an answer with searching?
    – Gumbo
    Jan 20, 2010 at 18:21
  • @Gumbo I searched SO, not google. Possibly not the correct term but I did search for "URL friendly username" with not much luck. I didn't know it was called a slug before this.
    – JasonDavis
    Jan 20, 2010 at 18:27
  • 1
    Maybe not everyone is trying to convert usernames. But searching for “URL friendly string” is returning usable results.
    – Gumbo
    Jan 20, 2010 at 19:04
  • Similar: stackoverflow.com/questions/5305879
    – GG.
    Jan 29, 2012 at 1:12
  • Nowadays, you can use libraries like github.com/cocur/slugify or github.com/ausi/slug-generator to achieve that.
    – ausi
    Oct 30, 2017 at 22:14

2 Answers 2

102
function Slug($string)
{
    // convert to entities
    $string = htmlentities( $string, ENT_QUOTES, 'UTF-8' );
    // regex to convert accented chars into their closest a-z ASCII equivelent
    $string = preg_replace( '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', $string );
    // convert back from entities
    $string = html_entity_decode( $string, ENT_QUOTES, 'UTF-8' );
    // any straggling caracters that are not strict alphanumeric are replaced with a dash
    $string = preg_replace( '~[^0-9a-z]+~i', '-', $string );
    // trim / cleanup / all lowercase
    $string = trim( $string, '-' );
    $string = strtolower( $string );
    return $string;
}

$user = 'Alix Axel';
echo Slug($user); // alix-axel

$user = 'Álix Ãxel';
echo Slug($user); // alix-axel

$user = 'Álix----_Ãxel!?!?';
echo Slug($user); // alix-axel
16
  • 9
    This is dangerous! Multiple unique user names can map to the same URL. That's not what you want, is it? Consider, e.g., AB and ab, which are unique strings but map to the same slug string. You should store the slug as the identifier. Jan 20, 2010 at 18:18
  • 12
    @John Feminella: He would obviously have to check for duplicates at some point before storing the slug.
    – Pekka
    Jan 20, 2010 at 18:19
  • 1
    Anyone know why Á and à would cause no output or error to occur when using this function?
    – John Conde
    Jan 20, 2010 at 18:42
  • 1
    Alex, thanks for the info. I reutnred the "UTF-8" parameter and saved the file as UTF-8 and it worked like a charm.
    – John Conde
    Jan 20, 2010 at 20:17
  • 3
    codepad demo of the Slug() function, with a second identical but spaced out nSlug() function (for the eyeball impaired): codepad.org/rJNSQmGJ Dec 13, 2012 at 8:02
3

In other words... you need to create a username slug. Doctrine (ORM for PHP) has a nice function to do it. Doctrine_Inflector::urlize()

EDIT: You should also keep username slug in database, as a Unique Key column. Then every search operation should be done based on that column, not original username.

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.