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
2010-01-20 18:16:47
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
2010-01-20 18:18:46
Nice one, Älix!
Pekka
2010-01-20 18:18:59
@John Feminella: He would obviously have to check for duplicates at some point before storing the slug.
Pekka
2010-01-20 18:19:48
perfect, thank you. BTW what does this part look for acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml ?
jasondavis
2010-01-20 18:19:49
@jasondavis: För stüff lıkë thîs!
Pekka
2010-01-20 18:20:22
As a slight improvement, using `iconv()` to convert to `ASCII//TRANSLIT` would probably catch a lot more chars.
Frank Farmer
2010-01-20 18:22:32
@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
2010-01-20 18:22:30
@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
2010-01-20 18:25:11
Anyone know why Á and à would cause no output or error to occur when using this function?
John Conde
2010-01-20 18:42:21
I'll answer my own question: remove the "'UTF-8'" parameter from htmlentities. That did the trick.
John Conde
2010-01-20 19:36:09
@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
2010-01-20 19:44:56
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
2010-01-20 20:17:22
@John Conde: No problem! ;) You should always save your files UTF-8 encoded.
Alix Axel
2010-01-20 21:07:25
+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
2010-01-20 18:18:51
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
2010-01-20 18:29:47