HDFilmMaker2112 Posted June 20, 2011 Share Posted June 20, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/239850-convert-letter-to-number-to-another-letter/ Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2011 Author Share Posted June 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239850-convert-letter-to-number-to-another-letter/#findComment-1232047 Share on other sites More sharing options...
priti Posted June 20, 2011 Share Posted June 20, 2011 I think you have to define the number to alpha and alpha to number array and perform the comparison. Quote Link to comment https://forums.phpfreaks.com/topic/239850-convert-letter-to-number-to-another-letter/#findComment-1232048 Share on other sites More sharing options...
kenrbnsn Posted June 20, 2011 Share Posted June 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239850-convert-letter-to-number-to-another-letter/#findComment-1232053 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2011 Author Share Posted June 20, 2011 no luck... still a blank page. http://www.makethemoviehappen.com/new.php Quote Link to comment https://forums.phpfreaks.com/topic/239850-convert-letter-to-number-to-another-letter/#findComment-1232057 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2011 Author Share Posted June 20, 2011 no luck... still a blank page. http://www.makethemoviehappen.com/new.php I apologize I had two files called the same thing and uploaded the wrong one. Your way is working. Quote Link to comment https://forums.phpfreaks.com/topic/239850-convert-letter-to-number-to-another-letter/#findComment-1232060 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2011 Author Share Posted June 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239850-convert-letter-to-number-to-another-letter/#findComment-1232077 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2011 Author Share Posted June 20, 2011 $letter['a']=01; $number['01']="0"; My GF pointed out that $letter['a']=01; should have quotes around the 01. $letter['a']="01"; Solved it. Quote Link to comment https://forums.phpfreaks.com/topic/239850-convert-letter-to-number-to-another-letter/#findComment-1232082 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.