Jump to content

Is this the best way to increment characters?


wee493

Recommended Posts

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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]);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.