我的HTML或PHP页面上显示的目录中的随机文章。我已经知道如何在HTML静态页面上执行此操作,但不是随机的

我的HTML或PHP页面上显示的目录中的随机文章。我已经知道如何在HTML静态页面上执行此操作,但不是随机的,php,spintax,Php,Spintax,我试图随机从目录中提取文章,然后旋转并显示。这是针对Spintax文章的示例: {猫|狗|猴|鱼|蜥蜴}{去湖边|吃了一只老鼠|跳进水里} 该目录将有多个文本文件。我敢肯定我差点就拿到了 <?php function spin($s){ preg_match('#\{(.+?)\}#is', $s, $m); if (empty($m)) return $s; $t = $m[1]; if

我试图随机从目录中提取文章,然后旋转并显示。这是针对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=文件内容($string,“r”)$绳子不属于我,我有点累了

  <?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);
                      }
                  ?>


您的代码有什么问题?txt文件中有什么$f=文件获取内容($string,“r”)?fopen()