onedumbcoder Posted May 21, 2008 Share Posted May 21, 2008 is there letter incrementer? for example lets say i have $letter = "A" $incrementedLetter = incrementLetter($letter);? Link to comment https://forums.phpfreaks.com/topic/106678-letter-incrementer/ Share on other sites More sharing options...
oopster Posted May 21, 2008 Share Posted May 21, 2008 Hello For example <?php function incrementLetter($letter) { $letters = array('a','b','c','e'); for($n=0; $n<=count($letter);$n++) { if(strtolower($letter)==$letters[$n]) return $letters[$n+1]; } } ?> Link to comment https://forums.phpfreaks.com/topic/106678-letter-incrementer/#findComment-546853 Share on other sites More sharing options...
marcusfaye87 Posted May 21, 2008 Share Posted May 21, 2008 don't know if there is a built in function, but if I had to make one: A through Z 0 through 25 <?php function incrementLetter($letter) { $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $postion = strpos ($letter, $letters); $position++; return substr ($letters,$position,1); } ?> UPDATE: if end is reached, jump to A (0) <?php function incrementLetter($letter) { $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $postion = strpos ($letter, $letters); $position++; if ($position == 26) { // if you've reached the last letter "Z", and want to go one further, it'll jump back to "A" $postion = 0; } return substr ($letters,$position,1); } ?> Link to comment https://forums.phpfreaks.com/topic/106678-letter-incrementer/#findComment-546854 Share on other sites More sharing options...
PFMaBiSmAd Posted May 21, 2008 Share Posted May 21, 2008 You can actually use the ++ increment operator - $letter = "A"; echo $letter; $letter++; echo $letter; Link to comment https://forums.phpfreaks.com/topic/106678-letter-incrementer/#findComment-546859 Share on other sites More sharing options...
marcusfaye87 Posted May 21, 2008 Share Posted May 21, 2008 You can actually use the ++ increment operator - $letter = "A"; echo $letter; $letter++; echo $letter; Man PHP is awesome BTW, figured out mine doesn't work. strpos returns a BOOLEAN response, not the actual position How do you get the position a letter is at in a string then? :s Link to comment https://forums.phpfreaks.com/topic/106678-letter-incrementer/#findComment-546861 Share on other sites More sharing options...
marcusfaye87 Posted May 21, 2008 Share Posted May 21, 2008 Mine failed because of a typo lol... very professional <?php function incrementLetter($letter) { $string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $position = strpos ($string, $letter); $position++; if ($position == 26) { // if you've reached the last letter "Z", and want to go one further, it'll jump back to "A" $postion = 0; } return substr ($string,$position,1); } $originalLetter = "A"; echo "The original letter is " . $originalLetter; echo "<br />"; echo "<br />"; $temp = $originalLetter; for ($i=0; $i<50; $i++) { $temp = incrementLetter($temp); echo $temp . " "; } ?> But if it works with just using the increment operator, what's the use haha Link to comment https://forums.phpfreaks.com/topic/106678-letter-incrementer/#findComment-546867 Share on other sites More sharing options...
trq Posted May 21, 2008 Share Posted May 21, 2008 Another method is the range(0 function. <?php foreach (range('a','z') as $letter) { echo $letter."\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/106678-letter-incrementer/#findComment-546894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.