我正在尝试从一个目录中随机提取文章,然后旋转和显示。这是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);
}
?>发布于 2015-06-09 14:49:47
我在20分钟后修复了这个问题。我在上面发布了一个没有工作的脚本。
$f = file_get_contents( $string,"r");$string不属于,我有点累了。
<?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");
$files = array_rand($articles);
$f = fopen($articles[$files], "r");
while ( $line = fgets($f, 1000) ) {
echo spin($line);
}
?>https://stackoverflow.com/questions/30701742
复制相似问题