Welcome to WebmasterWorld Guest from 3.80.4.76

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Encode a String Using an Array

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

New User

10+ Year Member

joined:Aug 24, 2007
posts:14
votes: 0


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

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

New User

10+ Year Member

joined:Aug 24, 2007
posts:14
votes: 0


Would this be the most efficient way to do this?

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

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

Full Member

10+ Year Member

joined:July 22, 2005
posts:202
votes: 0


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);

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members