Welcome to WebmasterWorld Guest from 54.226.46.6

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

str replace; Not to replace previously replaced strings

Using an array

     

itledi

11:00 am on Feb 15, 2008 (gmt 0)

5+ Year Member



Hello,

What I'm trying to do is a very simple obsification of a string.

Think of those secrete encoder/decoder alphabet codes people make in elementary school. Where A=>B, B=>E, C=>R, D=>A...

Here's what I have so far:

$message="ab";
$encoded=str_replace(array("a","b"),array("b","e"),$message);
echo $encoded;

What I would have liked as an output whould have been "be", but the current script then replaces the newly converted b to an e in the second replace to give an actual output of "ee".

How can I modify this script to not replace elements that have already been replaced?

Thanks

RonPK

11:34 am on Feb 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, try using strtr [php.net]:

echo strtr($message, 'ab', 'be');
 

Featured Threads

Hot Threads This Week

Hot Threads This Month