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?

+3


source to share


2 answers


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;

      

+1


source


Something like

$string = "This is random text in this string.";

$replace = array('s' => '$', 'o' => '0', 'i' => '1');
foreach(array_rand($replace, (count($replace)/2)+1) as $key) {
    $string = str_replace($key, $replace[$key], $string);
}

      



Demo .

+1


source







All Articles