justinh Posted December 10, 2008 Share Posted December 10, 2008 I wrote a code that finds what number in the alphabet a certain string is. Determined by what number the letter is in the alphabet, a function adds an ending to the number. (i.e for the letter A, the ending st is added to the end of 1) The code works fine, I was just wondering if there is an easier way to do it. <?php function editpos($position){ if(strlen($position) == 2){ if(strpos($position,"1") == 1 || strpos($position,"1",1) == 1){ $positionend = "th"; return $positionend; } if(strpos($position,"2") == 1 || strpos($position,"2",1) == 1){ $positionend = "nd"; return $positionend; } if(strpos($position,"1", 1) == 1){ $positionend = "st"; return $positionend; } if(strpos($position,"2", 1) == 1){ $positionend = "nd"; return $positionend; } if (strpos($position,"3", 1) == 1){ $positionend = "rd"; return $postionend; } if (strpos($position,"3", 1) !== 1 || strpos($position,"2", 1) !== 1 || strpos($position,"1", 1) !== 1){ $positionend = "th"; return $positionend; } } else { if($position == 1){ $positionend = "st"; return $positionend; } if($position == 2){ $positionend = "nd"; return $positionend; } if ($position == 3){ $positionend = "rd"; return $postionend; } if ($position !== 1 || $position !== 2 || $position !== 3){ $positionend = "th"; return $positionend; } } } $string = "abcdefghijklmnopqrstuvwxyz"; $lettertofind = "k"; if(strpos($string,$lettertofind) == 0){ echo "<b>".$lettertofind."</b> is the 1st letter in the alphabet"; } $position = strpos($string,$lettertofind) + 1; $positionend = editpos($position); echo "<b>".$lettertofind."</b> is the ".$position."".$positionend." letter in the alphabet"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136416-easier-way/ Share on other sites More sharing options...
trq Posted December 10, 2008 Share Posted December 10, 2008 function editpos($num) { if ($num == 0) { return 0; } if (in_array(($num % 100), range(11,13))) { return $num . 'th'; } else { switch (($num % 10)) { case 1: return $num . 'st'; break; case 2: return $num . 'nd'; break; case 3: return $num . 'rd'; break; default: return $num . 'th'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/136416-easier-way/#findComment-711870 Share on other sites More sharing options...
justinh Posted December 10, 2008 Author Share Posted December 10, 2008 Lol wow.. alot easier. Thanks thorpe, gonna study how you did this right now =P Quote Link to comment https://forums.phpfreaks.com/topic/136416-easier-way/#findComment-711879 Share on other sites More sharing options...
justinh Posted December 10, 2008 Author Share Posted December 10, 2008 does this add nd to 22? and th to 11? Quote Link to comment https://forums.phpfreaks.com/topic/136416-easier-way/#findComment-711882 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 does this add nd to 22? and th to 11? Try it out =) Notice the first if he checks for 11, so it should return correctly. Quote Link to comment https://forums.phpfreaks.com/topic/136416-easier-way/#findComment-711888 Share on other sites More sharing options...
trq Posted December 10, 2008 Share Posted December 10, 2008 does this add nd to 22? and th to 11? Indeed it does. I just realised however it only does half of what you asked. Heres a function that will return the number in the alphabet represented by a character. function findalphapos($letter) { if (strlen($letter) > 1) { return false; } $letter = strtolower($letter); $arr = array_combine(range('a','z'),range(1,26)); return $arr[$letter]; } You could now use both these together to do what you want. eg; echo editpos(findalhapos('k')); Quote Link to comment https://forums.phpfreaks.com/topic/136416-easier-way/#findComment-711890 Share on other sites More sharing options...
Mark Baker Posted December 10, 2008 Share Posted December 10, 2008 function findalphapos($letter) { if (strlen($letter) > 1) { return false; } return ord(strtoupper($letter)) - 64; } You could now use both these together to do what you want. eg; echo editpos(findalhapos('k')); Quote Link to comment https://forums.phpfreaks.com/topic/136416-easier-way/#findComment-711940 Share on other sites More sharing options...
justinh Posted December 10, 2008 Author Share Posted December 10, 2008 I still have alot to learn i guess =/ Quote Link to comment https://forums.phpfreaks.com/topic/136416-easier-way/#findComment-711951 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.