0

我有以下2个变量/字符串:

$article = ('Perfume is a mixture of fragrant word1 and aroma complexes, which can be applied to the human body, animals, word2, objects, and living spaces in order to give off a pleasant word3.');

$words = '{word1|word2|word3|word4|word5}';

我需要做的是在 $article 中搜索 $word 中的任何单词,如果找到的话,将在 $article 中找到的单词随机替换为从 $word 中随机挑选的单词。

请问我该怎么做?

4

2 回答 2

3

如果您需要将所有内容保持相同的格式;

$article = ('Perfume is a mixture of fragrant word1 and aroma complexes, which can be applied to the human body, animals, word2, objects, and living spaces in order to give off a pleasant word3.');
$words = '{word1|word2|word3|word4|word5}';

$words = explode('|', str_replace(array('{', '}'), '', $words));

echo str_replace($words, $words[array_rand($words)], $article);
于 2013-09-12T10:23:44.240 回答
0

创建一个 $words 数组。获取数组的随机键并将 $article 替换为随机选择的单词。

$words = array('word1', 'word2', 'word3', 'word4');
$randkeyA = array_rand($words);
$randkeyB = array_rand($words);
echo str_replace($words[$randkeyA], $words[$randkeyB], $article);
于 2013-09-12T10:17:42.723 回答