0

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

1 回答 1

0

我在 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);
                      }
                  ?>
于 2015-06-09T06:49:47.057 回答