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?
-
2There are plenty questions like this. Didn’t you get an answer with searching?– GumboJan 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.– JasonDavisJan 20, 2010 at 18:27
-
1Maybe not everyone is trying to convert usernames. But searching for “URL friendly string” is returning usable results.– GumboJan 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.– ausiOct 30, 2017 at 22:14
2 Answers
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
-
9This is dangerous! Multiple unique user names can map to the same URL. That's not what you want, is it? Consider, e.g.,
AB
andab
, 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.– PekkaJan 20, 2010 at 18:19
-
1Anyone know why Á and à would cause no output or error to occur when using this function? Jan 20, 2010 at 18:42
-
1Alex, thanks for the info. I reutnred the "UTF-8" parameter and saved the file as UTF-8 and it worked like a charm. Jan 20, 2010 at 20:17
-
3codepad demo of the
Slug()
function, with a second identical but spaced outnSlug()
function (for the eyeball impaired): codepad.org/rJNSQmGJ Dec 13, 2012 at 8:02
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.
-
Broken link... can now be found here: github.com/doctrine/inflector/blob/2.0.x/lib/Doctrine/Inflector/… Dec 21, 2022 at 22:36
-
Though, it's worth noting that this take the "keep a comprehensive list of all characters to replace" approach, which is much harder to maintain than the approach of the accepted answer. Dec 21, 2022 at 22:38