在PHP中随机化单词中间
前几天给我发送了一封电子邮件,其中包含一些文本,其中每个单词的开头和结尾字母都保留了下来,但每个单词的中间部分都是随机的。奇怪的是,文本仍然可读,这是由于大脑处理单词的方式所致。
我想知道是否可以使用PHP脚本来复制它。我需要做的就是将句子拆分为组成词,然后循环遍历这些词,使它们的中间位置随机化。显然,不可能在少于四个字符的单词中混合字母顺序,因此需要对此进行检查。这就是我的想法:
function mixWordMiddle($string)
{
$string = explode(' ', $string);
foreach ($string as $pos => $word) {
$tmpArray = array();
if (strlen($word) > 3) {
$chars = preg_split('//',$word,-1,PREG_SPLIT_NO_EMPTY);
for ($i = 1 ; $i < count($chars)-1 ; ++$i) {
$tmpArray[] = $chars[$i];
shuffle($tmpArray);
}
$string[$pos] = $chars[0] . implode($tmpArray) . $chars[count($chars)-1] . ' ';
}
}
echo implode(' ', $string);
}然后,我尝试插入以下有关进化的文字。
$string='Inbiology,evolutionisthechangesintheinheritedtraitsofapopulationoforganismsfromonegenerationtothenext.Thesechangesarecausedbyacombinationofthreemainprocesses:variation,reproduction,andselection.';
并提出了类似以下内容的内容。
Inbliygoo,eoutivolnisthecganhesintheiethirnedtitrasofapiaplouotnofoargnsimsformonegneoeatirntothenxte.Thseecagnhesareceusadbyacmibitoonanoftheremainpersocses:voaitanri,rteunodpoirc,andstoneleic.
这实际上是很难阅读的。我认为这可能是因为我使用了一些带有太多长词的文本,所以我选择了另一个:
$string='AgiantSaudioiltankerseizedbypiratesintheIndianOceanisnearingthecoastofSomalia,theUSNavysays.';
这产生了以下文本。
AganitSuadioiltaeknrseezidbyptaiersintheIanidnOecanisnraneigthecosatofSmiolaa,theUSNavysyas.
这只是一个测试脚本,因此不考虑任何标点符号。但是,它产生的文本仍然难以阅读,这使我对我收到的电子邮件的主张表示怀疑。