Characters replace ... need help
i am looking for a function to replace each character from a string with 开发者_Go百科a rand character from array.
lets say that i have a string
$s="Abc";
and i have an arrays 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
Assuming $sr[1] contains the possible letters for the first char, $sr[2] for the second char, etc as an array :
$s="Abc";
$sr[1]=array('À', 'Â', 'Á', 'Ã', 'Ä', 'Å', 'Ā', 'Ă', 'Ą', 'Д', 'à', 'á', 'â', 'ã', 'ä', 'å', 'ā', 'ă', 'ą', 'ǻ', 'α', '@');
$sr[2]=array('ß', 'β', 'Ђ', 'Б', 'Ъ', 'Ь', 'б', '฿');
$sr[3]=array('Ç', 'Ć', 'Ĉ', 'Ċ', 'Č', 'ć', 'ĉ', 'ċ', 'ς');
$newStr = "";
for ($i = 0, $len = strlen($s); $i < $len; $i++) {
$newStr .= $sr[$i + 1][array_rand($sr[$i + 1])];
}
echo $newStr;
Basically, we use the function array_rand to select a random index in every array for every character in the string.
If you absolutely need strings instead of arrays, you can work your way around with something like this :
$str = 'ÀÂ'; // etc.
$randomChar = mb_substr(str_shuffle($str), 0, 1);
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().
Edit
I assumed characters would always be in order. If it is not the case then yes, it's better to use an associative array :
$s="bAc";
$sr = array('A' => array('À', 'Â', 'Á', 'Ã', 'Ä', 'Å', 'Ā', 'Ă', 'Ą', 'Д', 'à', 'á', 'â', 'ã', 'ä', 'å', 'ā', 'ă', 'ą', 'ǻ', 'α', '@'),
'B' => array('ß', 'β', 'Ђ', 'Б', 'Ъ', 'Ь', 'б', '฿'),
'C' => array('Ç', 'Ć', 'Ĉ', 'Ċ', 'Č', 'ć', 'ĉ', 'ċ', 'ς'),
);
$newStr = "";
for ($i = 0, $len = strlen($s); $i < $len; $i++) {
$upperChar = strtoupper($s[$i]);
$newStr .= $sr[$upperChar][array_rand($sr[$upperChar])];
}
echo $newStr;
I can't think of any easy way to do this. You could do a switch.
You can also do an array with a key
$arr = array("A" => array('ÀÂÁÃÄÅĀĂĄДàáâãäåāăąǻα@');
depending on the case you, if you want upper and lower or they are the same output for both then convert to one case or another.
Then you could do $characterArray = $arr[currChar];
Then use rand to randomize from 0 to (characterArray length - 1) and your new character would be characterArray[randomNumber]
You can just append this to a new string.
You would have to loop through the string and go through each character, convert it and append to new string and output new string.
That would be the easiest thing i can think of.
You should check if the key exists or not though, if it doesn't exist i would just say keep the character that was passed in.
This will be your translation table. $arr = array( "A" => array('ÀÂÁÃÄÅĀĂĄДàáâãäåāăąǻα@', "B" => array('Allthebs'), "C" => array('Allthecs'), );
Say $str is the string being passed in.
So we loop through string
for(i = 0, i < strlen($str), i++){
$currChar = strtoupper($str[i]);
//May want to check if array key exists here, if it does then do whats below.
//else you would just append the currChar to the newStr.
$characterArr = $arr[$currChar];
$arrlen = count($characterArr);
$ranNum = rand(0, $arrlen - 1);
$newStr .= $characterArr[$ranNum];
}
It's a been a while since i programmed in PHP, so this may not be entirely correct, but you get the idea.
well, for starters something like array('ÀÂÁÃÄÅĀĂĄДàáâãäåāăąǻα@')
will just give you a single element array containing a string. But since you can treat strings like an array, you don't need an array at all.
// This would be easier if you label your replacement lists with the character they are replacing, for instance:
$replacements = array(
'a' => 'ÀÂÁÃÄÅĀĂĄДàáâãäåāăąǻα@',
'b' => 'ßβЂБЪЬб฿',
'c' => 'ÇĆĈĊČćĉċς',
etc...
);
// now you can iterate through each character of string and replace it with one randomly selected from the replacement string
$input_string = 'Abc';
$output_buffer = '';
for($i=0; $i<strlen($input_string); ++$i) {
// remember we used lower case keys in the $replacements hash
$key = strtolower($input_string[$i]);
if(isset( $replacements[$key] )) {
// if we have a replacement list for this key select a random char from it
$output_buffer .= $replacements[$key][rand(0, strlen($replacements[$key]))];
}
else {
// no replacement list found
$output_buffer .= $input_string[$i];
}
}
It looks like all your strings are UTF-8 or some other multibyte encoding, so Fayden's first solution is the only safe one I see.
If you used string index notation (e.g. $str[2]
gets you the 3rd byte) or substr
to pick a single character from a UTF-8 string, you'll just get a random byte (maybe a character, maybe garbage).
精彩评论