Replace random letters in the string with a suitable one
I'm trying to replace a letter or letters in a string with the appropriate letter, but I want it to be random.
$string = "This is random text in this string.";
Here are a few examples of the letter that I would change below
s = $
, o = 0
,i = l
I am trying to randomly replace the letters in this line above and the result looks like this. It would be all random and would not have to change all the letters just randomly to match the letters above.
Output examples
This is rand0m text in this $tring.
Thi$ is random text in this string.
How would I go about doing this in PHP?
source to share
Couldn't be more random than this. add to array $ a for more meaningful characters
$str = 'This is random text in this string.';
$str2 = '';
function repl($char){
$arr = array(
's' => '$',
't' => 'T'
);
foreach($arr as $key=>$a){
if($char == $key) return $a;
}
return $char;
}
foreach(str_split($str) as $char){
if(!rand(0, 9)) $str2 .= repl($char);
else $str2 .= $char;
}
echo $str."\n";
echo $str2;
source to share