I'm trying to extract articles from a directory at random and then spin and display. This is for Spintax articles Example:
{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);
}
?>