Al3in
October 22, 2010, 11:30pm
1
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.
Al3in
October 24, 2010, 9:47am
3
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;
Al3in
October 24, 2010, 8:18am
4
joebert:
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'] = 'ÀÂÁÃÄÅĀĂĄДàáâãäåāăąǻα@';
$sr['b'] = 'ßβЂБЪЬб฿';
$sr['c'] = 'ÇĆĈĊČćĉċς';
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.
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.
Al3in
October 23, 2010, 12:12am
5
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.
$sr['a'] = array('À','Â','Á','Ã','Ä','Å','&#256;','&#258;','&#260;','&#1044;','à','á','â','ã','ä','å','&#257;','&#259;','&#261;','&#507;','&#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'] = 'ÀÂÁÃÄÅ&#256;&#258;&#260;&#1044;àáâãäå&#257;&#259;&#261;&#507;&#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 ‘Ā ’, it doesn’t show the ‘&’ but it remains ‘&’ :injured:
what about
generate a random number between 0 and the length-1 of each $sr array
get the value of each $sr array at the index of the random number in 1)
concatenate the characters in 2) to get your new string.
Isn’t that what joebert’s script does?
Al3in
October 24, 2010, 10:09am
11
guido2004:
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'] = 'ÀÂÁÃÄÅ&#256;&#258;&#260;&#1044;àáâãäå&#257;&#259;&#261;&#507;&#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 ‘Ā ’, it doesn’t show the ‘&’ but it remains ‘&’ :injured:
yes i have the same problem …
joebert
October 23, 2010, 5:58pm
12
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'] = 'ÀÂÁÃÄÅĀĂĄДàáâãäåāăąǻα@';
$sr['b'] = 'ßβЂБЪЬб฿';
$sr['c'] = 'ÇĆĈĊČćĉċς';
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.