Jump to content

easier way?


justinh

Recommended Posts

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

?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/136416-easier-way/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/136416-easier-way/#findComment-711870
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/136416-easier-way/#findComment-711890
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/136416-easier-way/#findComment-711940
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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