0

我正在我的服务器上尝试这个新脚本,但我似乎无法弄清楚为什么它一直给我以下错误:

警告:file_get_contents():第 51 行 /includes/classes/spin_article.php 中的文件名不能为空

这一行给出了错误 --> $string = file_get_contents($files[rand(1, count($files)) -1]);

<?PHP
class Spintax
{
    public function process($text)
    {
        return preg_replace_callback(
            '/\{(((?>[^\{\}]+)|(?R))*)\}/x',
            array($this, 'replace'),
            $text
        );
    }

    public function replace($text)
    {
        $text = $this->process($text[1]);
        $parts = explode('|', $text);
        return $parts[array_rand($parts)];
    }
}
?>

<?PHP
ob_start();
$files = glob("spintax_articles/*.txt");
$spintax = new Spintax();
$string = file_get_contents($files[rand(1, count($files)) -1]);
echo $spintax->process($string);
$page = ob_get_contents();
ob_end_flush();
$fp = fopen("content.txt","w");
fwrite($fp,$page);
fclose($fp);
?>
4

1 回答 1

2

如果确实有匹配的文件,那么您可能会得到一个比最大值大 1 的随机索引。利用:

$string = file_get_contents($files[rand(1, count($files)-1)]);

注意-1放置。

并且以防万一没有文件,您应该使用条件进行检查。

<?php
$files = glob("spintax_articles/*.txt");

if(!empty($files)) {

    $spintax = new Spintax();
    $string  = file_get_contents($files[mt_rand(1, count($files)-1)]);

    $fp = fopen("content.txt", "w");
    fwrite($fp, $spintax->process($string));
    fclose($fp);
}
?>
于 2014-07-16T19:39:28.657 回答