views:

465

answers:

2

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?

+11  A: 
function Slug($string)
{
    return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}

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

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

$user = 'Álix----_Ãxel!?!?';
echo Slug($user); // alix-axel
Alix Axel
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.
John Feminella
Nice one, Älix!
Pekka
@John Feminella: He would obviously have to check for duplicates at some point before storing the slug.
Pekka
perfect, thank you. BTW what does this part look for acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml ?
jasondavis
@jasondavis: För stüff lıkë thîs!
Pekka
As a slight improvement, using `iconv()` to convert to `ASCII//TRANSLIT` would probably catch a lot more chars.
Frank Farmer
@Pekka: Thanks! =) @jasondavid: It removes accents, instead of having a huge lookup table we convert to html entities and fetch the unaccented char.
Alix Axel
@Frank Farmer: `iconv('UTF-8', 'ASCII//TRANSLIT', 'Álix Ãxel')` returns `'Alix ~Axel`. Using PHP 5.3.0 and .php file encoded as UTF-8 **no** BOM.
Alix Axel
Anyone know why Á and à would cause no output or error to occur when using this function?
John Conde
I'll answer my own question: remove the "'UTF-8'" parameter from htmlentities. That did the trick.
John Conde
@John Conde: I though you're talking about the `iconv()` function! You shouldn't remove the `UTF-8` from `htmlentities`, instead you should **save all your `.php` files encoded as UTF-8 no BOM**.
Alix Axel
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
@John Conde: No problem! ;) You should always save your files UTF-8 encoded.
Alix Axel
+1  A: 

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.

Crozin
I was planning on only storing the slug version, that will be there username. I didn't know it was called a slug though until I posted this question.
jasondavis