随机字符串旋转器问题



我想制作一个旋转功能来旋转字符串,但我会出现问题的问题。我已经知道如何搭配字符串中的单词如更改{hi | hello},但是我想要的是不同的是ranom spin

$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]

所以任何帮助人

问候

您可以尝试

$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, " nt");
    $x = 1;
    while ( $tok !== false ) {
        $textNew[] = $tok and $x == $p and $textNew[] = $words[array_rand($words, 1)];
        $tok = strtok(" ");
        $x ++;
    }
    return implode(" ", $textNew);
}

最新更新