james182 Posted April 15, 2009 Share Posted April 15, 2009 ok i have a random key generator which works fine but i need to split it into groups of 4 and seperated by "-". eg: 0000-0000-0000-0000-0000 <- this is may goal. Below is my code so far. function str_makerand($country, $state, $minlength, $maxlength, $useupper, $usespecial, $usenumbers){ $charset = "abcdefghijklmnopqrstuvwxyz"; if ($useupper) $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if ($usenumbers) $charset .= "0123456789"; if ($usespecial) $charset .= "~@#$%^*()_+-={}|]["; // Note: using all special characters this reads: "~!@#$%^&*()_+`-={}|\\]?[\":;'><,./"; if ($minlength > $maxlength){ $length = mt_rand ($maxlength, $minlength); }else{ $length = mt_rand ($minlength, $maxlength); for ($i=0; $i<$length; $i++){ $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; } } $seq = $country ."". $state ."". $key; // split string to XXXX-XXXX-XXXX-XXXX-XXXX return $seq; } Quote Link to comment https://forums.phpfreaks.com/topic/154165-split-key-prob/ Share on other sites More sharing options...
JasonLewis Posted April 15, 2009 Share Posted April 15, 2009 I suppose you could use something like: $str = "00000000000000000000"; echo implode("-", str_split($str, 4)); Quote Link to comment https://forums.phpfreaks.com/topic/154165-split-key-prob/#findComment-810423 Share on other sites More sharing options...
james182 Posted April 15, 2009 Author Share Posted April 15, 2009 Spot on, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/154165-split-key-prob/#findComment-810432 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.