【问题标题】:random string spinner issue随机字符串微调器问题
【发布时间】:2012-10-11 03:52:25
【问题描述】:

我想制作一个旋转函数来旋转一个字符串,但我遇到了问题。我已经知道怎么跳了 字符串中的单词,例如更改 {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]

请大家帮忙

问候

【问题讨论】:

    标签: php string


    【解决方案1】:

    你可以试试

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

    【讨论】:

      猜你喜欢
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多