首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随机字符串旋转问题

随机字符串旋转问题
EN

Stack Overflow用户
提问于 2012-10-20 21:16:47
回答 1查看 147关注 0票数 1

我想要做一个自旋函数来旋转一个字符串,但是我为它的实现寻找了一个问题。我知道如何在字符串中弹奏单词,比如改变{hi\hello},但我想要的是不同的,它是在字符串中旋转的

代码语言:javascript
复制
$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的虚拟文本。

所以任何帮助的人

问候

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-20 21:23:48

你可以试试

代码语言:javascript
复制
$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);

输出示例

代码语言:javascript
复制
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.

所用功能

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12992772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档