Forum Moderators: coopster

Message Too Old, No Replies

Encode a String Using an Array

         

aeae

12:38 am on Dec 2, 2007 (gmt 0)

10+ Year Member



Let's say I have a secrete code. A=1, B=2, C=3...

I put this into an array:

$code=array("A"=>1,"B"=>2,"C"=3);

How would I encode a string using this array?

$string_input="AABBCC";
... php functions ...
$string_output="112233";

Thanks

aeae

12:46 am on Dec 2, 2007 (gmt 0)

10+ Year Member



Would this be the most efficient way to do this?

$string_output=str_replace(array("A","B","C"),array(1,2,3),$string_input);

Mr_Fern

1:08 am on Dec 2, 2007 (gmt 0)

10+ Year Member



just a follow up on the first reply

if you're doing it in the way of $code = array('inKey1' => 'outKey1', 'inKey2' => 'outKey2', ... 'inKeyN' => 'outKeyN');

you can do

$string_output = str_replace(array_keys($code), $code, $string_input);