Jump to content

Character Counting Snippet


refiking

Recommended Posts

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

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

}

?>

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.