简体   繁体   English

我的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

I'm trying to extract articles from a directory at random and then spin and display. 我正在尝试从目录中随机提取文章,然后旋转并显示。 This is for Spintax articles Example: 这是针对Spintax文章的示例:

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

I fixed this 20 mins after. 我在20分钟后解决了这个问题。 I posted a none working Script above. 我在上面发布了一个无法正常工作的脚本。

$f = file_get_contents($string, "r"); $ f = file_get_contents($ string,“ r”); $string did not belong, I was a bit tired. $ 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);
                      }
                  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 高级自定义字段在类别页面上显示不正确。 我如何只限于分类页面? - Advanced custom fields displaying incorrectly on category pages. How do i limit to my taxonomy page only? 如何从我的 HTML 代码访问 public_html 目录中的 PHP 文件? - How do I access a PHP file in public_html directory from my HTML code? HTML和PHP:如何正确排列页面? - HTML and PHP: How do I arrange my pages correctly? 我想使用wwwcopy脚本从我的PHP内容创建几个静态html页面 - I want to create several static html pages from my PHP content using a wwwcopy script 如何用SEO为主的静态HTML平衡数据库留言板的POST / GET PHP页面? - How do I balance POST/GET PHP pages for a database message board with SEO-minded static HTML? 如何将数据从PHP发送到HTML文章 - How to send data from PHP into an HTML article 如何在wordpress页面上的html表单中包含PHP? - How do I include PHP in my html form on a wordpress page? 如何在常规 PHP/HTML 页面和 angular 页面之间共享 PHP session? - How do I share PHP session between regular PHP/HTML pages and an angular page? 如何防止 PHP sleep function 停止我的 HTML 页面 - How do I prevent PHP sleep function from stopping my HTML page 如何从服务器根目录在HTML页面中包含外部PHP文件? - How do I include a external PHP file in HTML page from my server root?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM