我正在尝试从目录中随机提取文章,然后旋转并显示。这是 Spintax 文章示例:
{猫|狗|猴子|鱼|蜥蜴} {去湖边|吃老鼠|跳水}
该目录将有多个文本文件。我确定我几乎拥有它!
<?php
function spin($s){
preg_match('#\{(.+?)\}#is', $s, $m);
if (empty($m))
return $s;
$t = $m[1];
if (strpos($t, '{') !== false)
{
$t = substr($t, strrpos($t,'{') + 1);
}
$parts = explode("|", $t);
$s = preg_replace("+\{".preg_quote($t)."\}+is",
$parts[array_rand($parts)], $s, 1);
return spin($s);
}
$articles = glob("test/*.txt");
$file = array_rand($articles);
$string = file_get_contents($articles[$file]);
$f = file_get_contents($string, "r");
while ($line = fgets($f, 1000)) {
echo spin($line);
}
?>