Jump to content

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

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.