Jump to content

convert letter to number to another letter.


HDFilmMaker2112

Recommended Posts

Looking for the best way to convert a string to a bunch of numbers and then those numbers to a new string.

 

 

Something like this:

$string="APWE";

A into 05, 05 into Y.

P into 36, 36 into A

W into 23, 23 into e

E into 13, 13 into P

 

So it will go from APWE to 05362313 to YAeP

 

Looking at str_split($numbers, 2) to get from the numbers to the letters, but do I need to have a full library of each letters corresponding number and then the corresponding letter to those numbers to convert back to the new letters?

Here's what I have right now:

 

$input = "YAPePAsdf";
$input = str_split($input);

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

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

echo $output;

 

above that is a bunch of code assigning a letter to a number in this format:

 

$letter['letter']=number 1-52;

 

This isn't displaying anything.

How about

<?php
$letters_numbers = array('A'=>'05','P'=>'36','W'=>'23','E'=>'13');
$numbers_letters = array('05'=>'Y','36'=>'A','23'=>'e','13'=>'P');
$str = 'APWE';
$new_str = '';
foreach (str_split($str) as $char) {
    $new_str .= $numbers_letters[$letters_numbers[$char]];
}
echo $new_str;
?>

Ken

I expanded this out to the whole alphabet lower and capital case + 0-9.

 

And it's now converting a 10 character string to 8 characters.

 

<?php
require_once 'llib.php';
require_once 'nlib.php';
$str = 'Asda12Ka12';
$new_str = '';
foreach (str_split($str) as $char) {
    $new_str .= $number[$letter[$char]];
}
echo $new_str;
?>

 

 

I've tracked it down to the lowercase a

$letter['a']=01;

 

$number['01']="0";

 

so instead of inserting 0 into the new string it just doesn't insert anything.

I end up with this:

 

q9eRSvRS

 

It should be:

 

q9e0RSv0RS

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.