homepage Welcome to WebmasterWorld Guest from 54.161.197.188
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
str replace; Not to replace previously replaced strings
Using an array
itledi




msg:3575623
 11:00 am on Feb 15, 2008 (gmt 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

 

RonPK




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

Hi, try using strtr [php.net]:

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved