1

我想制作一个旋转函数来旋转一个字符串,但我遇到了问题。我已经知道如何像更改 {hi|hello} 一样在字符串中添加单词,但我想要的是不同的,它是字符串中的随机旋转

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

所以任何帮助家伙

问候

4

1 回答 1

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);
}
于 2012-10-20T21:23:48.010 回答