Welcome to WebmasterWorld Guest from 54.224.121.67

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

str replace; Not to replace previously replaced strings

Using an array

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

Junior Member

5+ Year Member

joined:Dec 5, 2007
posts:107
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 22, 2002
posts:1751
votes: 0


Hi, try using strtr [php.net]:

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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members