I was trying to make a simple encryption routine when I found this strange behavior from str_replace(). It appears as though the function passes over the original string more than one time.
Is this normal?
Anyone know of a shortcut other than creating my own loops and recreating the str_replace() function?
<?php
function MyEncrypt($TextIn){
$OldArray = array("d","c","b","a");
$NewArray = array("a","b","c","d");
$TextOut = str_replace($OldArray, $NewArray, $TextIn);
return($TextOut);
}
function MyDecrypt($TextIn){
$OldArray = array("d","c","b","a");
$NewArray = array("a","b","c","d");
$TextOut = str_replace($NewArray, $OldArray, $TextIn);
return($TextOut);
}
$TextIn = "aaa bbb ccc ddd abcd";
$NewText = MyEncrypt($TextIn);
$OldText = MyDecrypt($NewText);
echo $TextIn."\n";
echo $NewText."\n";
echo $OldText."\n";
/
Outputs:
aaa bbb ccc ddd abcd
ddd ccc ccc ddd dccd
aaa bbb bbb aaa abba
/
/
I expected:
aaa bbb ccc ddd abcd
ddd ccc bbb aaa dcba
aaa bbb ccc ddd abcd
/
?>