Characters replace ... need help

Hello there

i am looking for a function to replace each character from a string with a rand character from array.

lets say that i have a string


$s="Abc";

and i have an arraies for each character like


$sr[1]=array('ÀÂÁÃÄÅĀĂĄДàáâãäåāăąǻα@');
$sr[2]=array('ßβЂБЪЬб฿');
$sr[3]=array('ÇĆĈĊČćĉċς');

how can i change the $s string to ( ÀβČ ) or ( ДбĈ ) …ect. on each refresh for the web browser.

Regards

Did you try joebert’s code? It looks like it should do the trick.

i did try it but it is giving me a strange output

when i tried to accent ( abc ) it give me ( @‘‰ )

this is the full code


$s="abc"; 
$sr['a'] = 'ÀÂÁÃÄÅĀĂĄДàáâãäåāăąǻα@'; 
$sr['b'] = 'ßβЂБЪЬб฿'; 
$sr['c'] = 'ÇĆĈĊČćĉċς';
for($i = 0, $toi = strlen($s); $i < $toi; $i++){
$k = strtolower($s[$i]);
$new_s .= $sr[$k][floor(lcg_value() * strlen($sr[$k]))];
}
echo $new_s; 

let me explain it to you…

lets say that i want to add an accent to a nick name ( ŢнỂβǑާ ). i want a function to convert any normal character to an accent character from the array.

No i want the ( Abc ) replaced not the (A) only

Can “A” only be replaced by values within array(‘ÀÂÁÃÄÅĀĂĄДàáâãäåāăąǻα@’) ?

In that case, what I would do is upload that script and then look at the resulting files source. Then I would rework my array-of-strings into an array-of-arrays so I could use the HTML-entities the file manager is generating.

For instance with that $sr[‘a’] line I’d end up with this.
You’ll have to remove the instances of “amp;” from what I’ve posted here, Sitepoint converts the entities to characters if I post the entities, and it converts the ampersand if I try to escape it. :slight_smile:

$sr['a'] = array('&#192;','&#194;','&#193;','&#195;','&#196;','&#197;','&amp;#256;','&amp;#258;','&amp;#260;','&amp;#1044;','&#224;','&#225;','&#226;','&#227;','&#228;','&#229;','&amp;#257;','&amp;#259;','&amp;#261;','&amp;#507;','&amp;#945;','@');

// Edit – Then again, your file managers are probably going to do the same thing Sitepoint is. I don’t have an answer for you in that case.

I tested it, and the replacing itself works fine. But when I save the script (I’m using the host’s online file manager), some of the characters get changed like this:


  $sr['a'] = 'ÀÂÁÃÄÅ&amp;#256;&amp;#258;&amp;#260;&amp;#1044;àáâãäå&amp;#257;&amp;#259;&amp;#261;&amp;#507;&amp;#945;@'; 

So the replacement routine uses numbers, &, # ecc. as well.
Maybe you’re having a similar problem?

Darn, if I write ‘& #256;’ (had to add a space to prevent the character from showing) it shows the character, if I write ‘&#256’, it doesn’t show the ‘&’ but it remains ‘&’ :injured:

what about

  1. generate a random number between 0 and the length-1 of each $sr array

  2. get the value of each $sr array at the index of the random number in 1)

  3. concatenate the characters in 2) to get your new string.

Isn’t that what joebert’s script does?

yes i have the same problem …

I’m not sure whether I understand the question, but this is what I think you’re asking for.

Use an associative array instead of a numerically indexed array.

$sr['a'] = '&#192;&#194;&#193;&#195;&#196;&#197;&#256;&#258;&#260;&#1044;&#224;&#225;&#226;&#227;&#228;&#229;&#257;&#259;&#261;&#507;&#945;@'; 
$sr['b'] = '&#223;&#946;&#1026;&#1041;&#1066;&#1068;&#1073;&#3647;'; 
$sr['c'] = '&#199;&#262;&#264;&#266;&#268;&#263;&#265;&#267;&#962;';

Then you can easily find the set of replacements you’re looking for.

for($i = 0, $toi = strlen($s); $i < $toi; $i++)
{
    $k = strtolower($s[$i]);
    $new_s .= $sr[$k][floor(lcg_value() * strlen($sr[$k]))];
}

You may need to use mb_strlen and friends to slice the string instead of the strlen if you’re working with multi-byte characters.