wee493 Posted July 22, 2010 Share Posted July 22, 2010 Currently I'm incrementing letters, for example aa, ab, ac ... az, ba. But I would like to make it so it goes aa, Aa, AA, aA, bb, Bb, BB... I'm halfway through making the code below, but I though I would ask if there is a better/easier way to do this? I would like to be able to support strings longer then 2 or three characters. Maybe a function or something? $prev = 'aa'; // Capitalize fist letter if not and second letter not if(!ctype_upper(substr($prev, 0, 1)) && !ctype_upper(substr($prev, 1, 2))){ $part_one = strtoupper(substr($prev, 0, 1)); $part_two = substr($prev, 1); $prev = $part_one.$part_two; } // Un-Capitalize first letter if first and second are if(ctype_upper(substr($prev, 0, 2))){ $part_one = strtolower(substr($prev, 0, 1)); $part_two = substr($prev, 1); $prev = $part_one.$part_two; } // Capaitalize second letter if first is already if(ctype_upper(substr($prev, 0, 1)) && !ctype_upper(substr($prev, 1, 2))){ $part_one = strtoupper(substr($prev, 0, 2)); $part_two = substr($prev, 2); $prev = $part_one.$part_two; } echo $prev; Quote Link to comment https://forums.phpfreaks.com/topic/208570-is-this-the-best-way-to-increment-characters/ Share on other sites More sharing options...
gwolgamott Posted July 22, 2010 Share Posted July 22, 2010 It's small and clean. I see nothing wrong with that method. If you want to do a function then it's just dumping your code there into a function and returning an array to the main area you are using this would clean up the main code area if you liked. Quote Link to comment https://forums.phpfreaks.com/topic/208570-is-this-the-best-way-to-increment-characters/#findComment-1089782 Share on other sites More sharing options...
simshaun Posted July 22, 2010 Share Posted July 22, 2010 This could possibly be made more efficient, but I had to throw it together quickly. <?php echo "<pre>"; print_r(getLetterPermutations('a')); echo "</pre>"; function getLetterPermutations($letter, $desiredLength = 2) { $possiblePerms = pow(strlen($letter) * 2, $desiredLength); $permutations = array($string = str_repeat($letter, $desiredLength)); $strlen = strlen($string); for ($p = 1; $p <= $possiblePerms; $p++) { for ($x = 0; $x < $strlen; $x++) { $string[$x] = $string[$x] === strtoupper($string[$x]) ? strtolower($string[$x]) : strtoupper($string[$x]); if (!in_array($string, $permutations)) $permutations[] = $string; } } return $permutations; } Quote Link to comment https://forums.phpfreaks.com/topic/208570-is-this-the-best-way-to-increment-characters/#findComment-1089801 Share on other sites More sharing options...
wee493 Posted July 23, 2010 Author Share Posted July 23, 2010 This could possibly be made more efficient, but I had to throw it together quickly. <?php echo "<pre>"; print_r(getLetterPermutations('a')); echo "</pre>"; function getLetterPermutations($letter, $desiredLength = 2) { $possiblePerms = pow(strlen($letter) * 2, $desiredLength); $permutations = array($string = str_repeat($letter, $desiredLength)); $strlen = strlen($string); for ($p = 1; $p <= $possiblePerms; $p++) { for ($x = 0; $x < $strlen; $x++) { $string[$x] = $string[$x] === strtoupper($string[$x]) ? strtolower($string[$x]) : strtoupper($string[$x]); if (!in_array($string, $permutations)) $permutations[] = $string; } } return $permutations; } Thank you so much! I've made a few minor changes and adapted it to my need. The efficiency is not a really problem, I most likely will never go over four characters as (if my math is correct) this should give me almost 3,000,000 unique results. I'm getting better at PHP, just need to learn a little bit more about lines like this $string[$x] = $string[$x] === strtoupper($string[$x]) ? strtolower($string[$x]) : strtoupper($string[$x]); Quote Link to comment https://forums.phpfreaks.com/topic/208570-is-this-the-best-way-to-increment-characters/#findComment-1089978 Share on other sites More sharing options...
Alex Posted July 23, 2010 Share Posted July 23, 2010 That's the ternary operator. Quote Link to comment https://forums.phpfreaks.com/topic/208570-is-this-the-best-way-to-increment-characters/#findComment-1089979 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.