PHP :: Convert a string to NATO alphabet

Giving a password over the phone to someone is always painful, and I can never remember the NATO alphabet (Whiskey Tango Foxtrot!). The following PHP function will convert any string into the NATO alphabet for easy recitation.

function convert_to_nato($word) {
     $lib['a']="Alpha";
     $lib['b']="Bravo";
     $lib['c']="Charlie";
     $lib['d']="Delta";
     $lib['e']="Echo";
     $lib['f']="Foxtrot";
     $lib['g']="Golf";
     $lib['h']="Hotel";
     $lib['i']="India";
     $lib['j']="Juliet";
     $lib['k']="Kilo";
     $lib['l']="Lima";
     $lib['m']="Mike";
     $lib['n']="November";
     $lib['o']="Oscar";
     $lib['p']="Papa";
     $lib['q']="Quebec";
     $lib['r']="Romeo";
     $lib['s']="Sierra";
     $lib['t']="Tango";
     $lib['u']="Uniform";
     $lib['v']="Victor";
     $lib['w']="Whiskey";
     $lib['x']="X-Ray";
     $lib['y']="Yankee";
     $lib['z']="Zulu";
     $lib['0']="Zero";
     $lib['1']="One";
     $lib['2']="Two";
     $lib['3']="Three";
     $lib['4']="Four";
     $lib['5']="Five";
     $lib['6']="Six";
     $lib['7']="Seven";
     $lib['8']="Eight";
     $lib['9']="Nine";
     $lib['-']="Dash";

     $nato=array();

     for($i=0;$i
          $letter=substr($word,$i,1);
          if (!empty($lib[$letter])) {
               $nletter=strtolower($lib[$letter]);
          } else {
               if (!empty($lib[strtolower($letter)])) {
                    $nletter=strtoupper($lib[strtolower($letter)]);
               } else {
                    $nletter=$letter;
               }
          }
          $nato[]=$nletter;
     }

     return implode(" ",$nato);
}
  1. Good guy coder

    In case anyone comes across this, the for loop is missing a piece after the $i because the author used a less than sign that got eaten by his blog.

    The for loop should be:

    for($i=0;$i<strlen($word);$i++) {

Leave a Comment