Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming $sr[1] contains the possible letters for the first char, $sr[2] for the second char, etc <strong>as an array</strong> :</p> <pre><code>$s="Abc"; $sr[1]=array('À', 'Â', 'Á', 'Ã', 'Ä', 'Å', 'Ā', 'Ă', 'Ą', 'Д', 'à', 'á', 'â', 'ã', 'ä', 'å', 'ā', 'ă', 'ą', 'ǻ', 'α', '@'); $sr[2]=array('ß', 'β', 'Ђ', 'Б', 'Ъ', 'Ь', 'б', '฿'); $sr[3]=array('Ç', 'Ć', 'Ĉ', 'Ċ', 'Č', 'ć', 'ĉ', 'ċ', 'ς'); $newStr = ""; for ($i = 0, $len = strlen($s); $i &lt; $len; $i++) { $newStr .= $sr[$i + 1][array_rand($sr[$i + 1])]; } echo $newStr; </code></pre> <p>Basically, we use the function array_rand to select a random index in every array for every character in the string.</p> <p>If you absolutely need strings instead of arrays, you can work your way around with something like this :</p> <pre><code>$str = 'ÀÂ'; // etc. $randomChar = mb_substr(str_shuffle($str), 0, 1); </code></pre> <p>As a few others pointed, you can use associative arrays. This problem can be solved using many solutions, it might be good to explain precisely what you need to do. mrclay also pointed out something very important, it may be a better idea to use mb_substr() (multibyte substring, literally) instead of substr().</p> <hr> <h2>Edit</h2> <p>I assumed characters would always be in order. If it is not the case then yes, it's better to use an associative array :</p> <pre><code>$s="bAc"; $sr = array('A' =&gt; array('À', 'Â', 'Á', 'Ã', 'Ä', 'Å', 'Ā', 'Ă', 'Ą', 'Д', 'à', 'á', 'â', 'ã', 'ä', 'å', 'ā', 'ă', 'ą', 'ǻ', 'α', '@'), 'B' =&gt; array('ß', 'β', 'Ђ', 'Б', 'Ъ', 'Ь', 'б', '฿'), 'C' =&gt; array('Ç', 'Ć', 'Ĉ', 'Ċ', 'Č', 'ć', 'ĉ', 'ċ', 'ς'), ); $newStr = ""; for ($i = 0, $len = strlen($s); $i &lt; $len; $i++) { $upperChar = strtoupper($s[$i]); $newStr .= $sr[$upperChar][array_rand($sr[$upperChar])]; } echo $newStr; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload