随机文章从目录显示在我的HTML或PHP页面.我已经知道如何在HTML静态页面上做到这一点,但不是随机的


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

我试图随机从目录中提取文章,然后旋转并显示。这是用于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);
    }
?>

我修复了这个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);
                      }
                  ?>