用php从文章中随机选择单词


Selecting random words from article with php

我想创建一个脚本来为我的产品生成不同的内容。例如,我的第一个产品内容是:

Acer 5520G笔记本电脑采用15.6液晶面板。

第二个是;

Acer 5720G笔记本电脑使用15.6屏幕。

我想创建我的文章像:

Acer(5520G|5720G)(笔记本电脑|笔记本电脑)使用15.6(液晶面板|屏幕)

然后,用php将(..)中写的所有单词随机化。

我用随机词;

$strings = '5520G, 5720G';
$key = array_rand($strings);
echo $strings[$key].

但是我无法从文章中选出(..)个单词,有什么建议吗?

你能使用JSON吗?

$items = json_decode('["Acer",["5520G","5720G"],["Laptop","Notebook"],"using 15.6",["LCD Panel","Screen"]]', true);
$out = array();
foreach($items as $item){
    if(is_array($item)){
        shuffle($item);
        $out[] = $item[0];
    }else{
        $out[] = $item;
    }
}
echo implode(' ', $out);

您正试图在字符串上使用数组函数。试试这个:

$strings = array('5520G', '5720G');
$key = array_rand($strings);
echo $strings[$key];