我想要做一个自旋函数来旋转一个字符串,但是我为它的实现寻找了一个问题。我知道如何在字符串中弹奏单词,比如改变{hi\hello},但我想要的是不同的,它是在字符串中旋转的
$spin_words=array('Word1','Word2','Word3');
$text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";所以我想随机添加一些单词,比如
Lorem是印刷和排版行业的Word1虚拟文本。
或
Lorem只是印刷、、Word2、和排版行业的虚拟文本。
或
Lorem只是印刷和排版行业Word3的虚拟文本。
所以任何帮助的人
问候
发布于 2012-10-20 21:23:48
你可以试试
$words = array('Word1','Word2','Word3');
$text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
echo wordInString($text,$words);
echo wordInString($text,$words);输出示例
Lorem Ipsum is simply dummy Word1 text of the printing and typesetting industry.
Lorem Ipsum is simply dummy text of the printing Word2 and typesetting industry.所用功能
function wordInString($text,array $words) {
$textNew = array();
$p = mt_rand(0, substr_count($text, " "));
$tok = strtok($text, " \n\t");
$x = 1;
while ( $tok !== false ) {
$textNew[] = $tok and $x == $p and $textNew[] = $words[array_rand($words, 1)];
$tok = strtok(" ");
$x ++;
}
return implode(" ", $textNew);
}https://stackoverflow.com/questions/12992772
复制相似问题