Help - Search - Members - Calendar
Full Version: Changing A Sting Of Letters Into Numbers And Back Again.
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
LancerB1
I want to know how to turn a string of letters (like: HELLO MY NAME IS LANCER) into a numeric value, then alter it by adding 1 or subtracting 5, and finally converting it back to a string. I'll be using Upper Case for the letters that I'm going to input in a form by using the method post.

This is the array that I'm going to use if this work to accomplish my goal.

CODE
<?php

$letter[''] = 00;
$letter['A'] = 01;
$letter['B'] = 02;
$letter['C'] = 03;
$letter['D'] = 04;
$letter['E'] = 05;
$letter['F'] = 06;
$letter['G'] = 07;
$letter['H'] = 08;
$letter['I'] = 09;
$letter['J'] = 10;
$letter['K'] = 11;
$letter['L'] = 12;
$letter['M'] = 13;
$letter['N'] = 14;
$letter['O'] = 15;
$letter['P'] = 16;
$letter['Q'] = 17;
$letter['R'] = 18;
$letter['S'] = 19;
$letter['T'] = 20;
$letter['U'] = 21;
$letter['V'] = 22;
$letter['W'] = 23;
$letter['X'] = 24;
$letter['Y'] = 25;
$letter['Z'] = 26;

$input = $_POST['string_input'];

~~Process the input into a numeric value and back into a string.~~

echo *Result*;

?>


This is my table that I set up to have someone input the information.

CODE
<form action="" method="post" >

<input type="text" name="string_input" /><br />
<input type="submit" value="Convert" style="width:152px;"/>
</form>


Thank you for your time.
swordz
CODE
$input = strtoupper($input);
$input = str_split($input);

foreach($input as $key=>$char) if(isset($letter[$char])) $input[$key] = $letter[$char];

$output = implode("", $input);


Swordz
Ed
I had a different approach to do this by using the ASCII table to form the values.

I think it's pretty self explanatory, but basically ord() gives you the ASCII decimal value of the character, decimal 65 - 90 are A-Z (all uppercase), by the index - 64 gives you the alphabet index (1 - 26).

CODE
<?php
function charToAlphIndex($char)
{
   $char = strtoupper($char);
   $ord = ord($char);

   // Ensure is A - Z
   return ($ord > 64 && $ord < 91) ? ($ord - 64) : false;
}

function alphIndexToChar($index)
{
   return ($index > 0 && $index < 27) ? chr($index + 64) : false;
}

$str = strtoupper('abcdefghijklmnopqrstuvwxyz');

for($i = 0; $i < strlen($str); $i++)
{
   $alphIndex = charToAlphIndex($str[$i]);
   echo $alphIndex, " ", alphIndexToChar($alphIndex), "\n";
}
?>


It should be noted that it won't handle anything other than A - Z, so you might want to factor that in if required.

Out of curiosity, what did you need it for?
LancerB1
Great, thanks for your help guys. Ok, this is what I have to turn the number back into a letter or character.

CODE
$number['01'] = 'A';
$number['02'] = 'B';
$number['03'] = 'C';
$number['04'] = 'D';
$number['05'] = 'E';
$number['06'] = 'F';
$number['07'] = 'G';
$number['08'] = 'H';
$number['09'] = 'I';
$number['10'] = 'J';
$number['11'] = 'K';
$number['12'] = 'L';
$number['13'] = 'M';
$number['14'] = 'N';
$number['15'] = 'O';
$number['16'] = 'P';
$number['17'] = 'Q';
$number['18'] = 'R';
$number['19'] = 'S';
$number['20'] = 'T';
$number['21'] = 'U';
$number['22'] = 'V';
$number['23'] = 'W';
$number['24'] = 'X';
$number['25'] = 'Y';
$number['26'] = 'Z';

?>


I haven't tested the script for yours Ed, but I'll do so when I finish tryout swordz script. This is what I have to turn it back again into a string with letters or characters from the associative array of letters or characters that I'm current working on.

CODE
<?php
........

$Numbinput = $_POST['numb_input'];
$Numbinput = strtoupper($Numbinput);
$Numbinput = str_split($Numbinput);

foreach($Numbinput as $key=>$char) if(isset($number[$char])) $Numbinput[$key] = $number[$char];
$output1 = implode("", $Numbinput);

echo $output1;

?>

<form action="" method="post" >
<input type="text" name="numb_input" /><br />
<input type="submit" value="Convert" />
</form>


For your question Ed, my goal is to create something similar to the Enigma Machine.
swordz
I used a default of str_split. You need to specify str_split($in, 2);

Swordz
LancerB1
Hm, I don't get what you are trying to say. If want to split an integer, I'll use str_split($n,2); to split it into 2 numbers?
swordz
You had e.g AWEDF. str_split on that would turn it to A-W-E-D-F, which the code then turns into 0123050406.

Str_split on that turns it into 0-1-2-3-0-5-0-4-0-6 which isn't what you want. str_split($n,2) turns it into 01-23-05-04-03 which is what you want.

Swordz
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.