随机字符串微调器问题

时间:2012-10-20 21:16:48

标签: php string

我想制作一个旋转函数来旋转一个字符串,但我做了这个问题。我已经知道怎么做了 字符串中的单词就像改变{hi | hello}但我想要的是不同的是字符串中的ranom旋转

$spin_words=array('Word1','Word2','Word3');
$text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";

所以我想像

一样随机添加单词
  

Lorem Ipsum只是 [Word1] 打印的虚拟文本和   排版行业。

  

Lorem Ipsum只是打印 [Word2] 和   排版行业。

  

Lorem Ipsum只是简单的打印文本和   排版行业 [Word3]

所以任何帮助人员

问候

1 个答案:

答案 0 :(得分:0)

你可以尝试

$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);
}