refiking Posted October 31, 2008 Share Posted October 31, 2008 I am trying to insert a snippet of code into my current script. I need to computer generate a username for the user based on the $name variable. If the $name variable is 8 characters, then $name = $username. If the $name variable is more than 8 characters, the first 8 characters of $name = $username. And if there are less than 8 characters, I'd like to computer generate enough characters to the end of the $name variable and make that $username. Just looking for some direction guys. I tried to google this, but came up with nothing (probably because I'm not even sure what this is called). Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/130917-character-counting-snippet/ Share on other sites More sharing options...
Andy17 Posted October 31, 2008 Share Posted October 31, 2008 Well, just to get you started, you could use strlen() to count the number of characters. <?php if (strlen($name) == { // $name is 8 characters long } elseif (strlen($name) > { // $name is more than 8 characters long } else { // $name is less than 8 characters long } ?> Link to comment https://forums.phpfreaks.com/topic/130917-character-counting-snippet/#findComment-679560 Share on other sites More sharing options...
rhodesa Posted October 31, 2008 Share Posted October 31, 2008 $chars = range('a','z'); shuffle($chars); $username = substr($name.$chars,0,; Link to comment https://forums.phpfreaks.com/topic/130917-character-counting-snippet/#findComment-679563 Share on other sites More sharing options...
refiking Posted October 31, 2008 Author Share Posted October 31, 2008 $chars = range('a','z'); shuffle($chars); $username = substr($name.$chars,0,; The $name variable is alphanumeric. How would I allow numbers as well? Link to comment https://forums.phpfreaks.com/topic/130917-character-counting-snippet/#findComment-679565 Share on other sites More sharing options...
kenrbnsn Posted October 31, 2008 Share Posted October 31, 2008 <?php $chars = array_merge(range('a','z'),range('0','9')); shuffle($chars); $username = substr($name.$chars,0,; ?> Ken Link to comment https://forums.phpfreaks.com/topic/130917-character-counting-snippet/#findComment-679570 Share on other sites More sharing options...
refiking Posted October 31, 2008 Author Share Posted October 31, 2008 OK. So, that code will reduce the size of the $name variable and that's great. I also need to autogenerate the remaining characters if the $name variable was not long enough. So, here's what I added (if anyone else ever needed something like this) $a = md5(uniqid(rand(), true)); $newname = $name . $a; $chars = array_merge(range('a','z'),range('0','9')); shuffle($chars); $username = substr($newname.$chars,0,; This way, if it already has 8 or more characters, it doesn't matter. But if it has less, it automatically adds the other characters on. Thanks gurus for the help. You guys ROCK! Link to comment https://forums.phpfreaks.com/topic/130917-character-counting-snippet/#findComment-679578 Share on other sites More sharing options...
rhodesa Posted October 31, 2008 Share Posted October 31, 2008 the original script i posted DID do that... Link to comment https://forums.phpfreaks.com/topic/130917-character-counting-snippet/#findComment-679585 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.