[英]Random Article from directory displayed on my HTML or PHP pages. I already know how to do this on an HTML Static page but not randomly
I'm trying to extract articles from a directory at random and then spin and display. 我正在尝试从目录中随机提取文章,然后旋转并显示。 This is for Spintax articles Example:
这是针对Spintax文章的示例:
{Cat|Dog|Monkey|Fish|Lizard} {went to the lake|ate a mouse|jumped in the water} {猫|狗|猴子|鱼|蜥蜴} {去湖边|鼠标|跳入水中}
The directory will have multiple text files. 该目录将包含多个文本文件。 I'm sure that I almost have it!
我敢肯定我快拿到了!
<?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);
}
?>
I fixed this 20 mins after. 我在20分钟后解决了这个问题。 I posted a none working Script above.
我在上面发布了一个无法正常工作的脚本。
$f = file_get_contents($string, "r"); $ f = file_get_contents($ string,“ r”); $string did not belong, I was a bit tired.
$ 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);
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.