2011-08-21 29 views
0

例如:将随机字符转换成字符串

$myString = "Hello World"; 

我希望把随机字符到它,所以它变成是这样的:

$myString = "[email protected]#llo$*Wo-rld+"; 

感谢

+1

好的,有很多方法可以做到这一点,但为什么? – Gerry

+0

@Gerry只是做一个新手加密。请帮助我:) – Muazam

+0

嗯,看看这个例子,如何做一次,然后你只需要在一个循环中运行你想插入的字符数:http://stackoverflow.com/questions/6503176/php-how-to-add-a-random-character-in-a-string-at-random-position – Gerry

回答

0

天真的算法:

$len=strlen($myString); // we will not recalc it 
$allowed=array('!','~'); 
for(int $i=0;$i<$countOfChars;++i){ 
    $x=rand(0,$len); //put after $x characters; 
    $char=$allowed[array_rand($allowed)]; 
    $myString=substr($myString,0,$x).$char.substr($myString,$x); 
    ++$len; 
} 

但是我认为你应该忘记这个想法,并以其他方式解决你的问题

+0

感谢它工作:) – Muazam