【问题标题】:Create variable $pathinfo and $random to get block 15 random lines of html file创建变量 $pathinfo 和 $random 以获取 html 文件的块 15 随机行
【发布时间】:2015-03-27 18:57:44
【问题描述】:

我在一个 php 网站上工作,想从一个 html 文件中回显 15 个随机行。到目前为止,我只有 15 行,但问题是我从 html 文件中获取代码,而不是从文本中获取 15 行。 我如何定义 2 个变量 $pathinfo 和 $random 来编写我的代码,从而获得正确的解决方案?

我的代码:

      <?php  

        $selected_file = $_POST['radio1'];
        // get the filename of the file
        $fileinfo = pathinfo($selected_file);
        $filename = $fileinfo['dirname'] . DIRECTORY_SEPARATOR . $fileinfo['filename'];
        $password = 'code';




    if (isset($_POST['submitradio']))
    { 
         echo '<div class="imageselected">';
         echo '<img src="'.$selected_file.'" /></br>'.PHP_EOL;
         echo '</div>';
        // check to see if a html file named the same also exists
        if(file_exists("$filename.html"))
        {

            echo "<form action='test_result.php' method='post'>";
            echo '<div class="Password">';
            echo 'Type in password to view full Script';

            echo "<label><div class=\"Input\"><input type='password' name='passIT' value='passit'/></div>";
            echo "<input type='submit' name='submitPasswordIT' value='Submit Password'/></div>";
            echo '</div>';
            echo '</form>';
            echo "$filename.html shares the same name as $selected_file";



        $splitTag = "[[mylinebreak]]";
        $html = file_get_contents("$filename.html");
        $newcontent = stri_replace("</div>", $splitTag, $html);
        $newcontent = stri_replace("<br>", $splitTag, $newcontent);
        $newcontent = stri_replace("</h1>", $splitTag, $newcontent);
        $newcontent = stri_replace("</h2>", $splitTag, $newcontent);
        $newcontent = stri_replace("</h3>", $splitTag, $newcontent);
        $newcontent = stri_replace("</h4>", $splitTag, $newcontent);
        $newcontent = stri_replace("</h5>", $splitTag, $newcontent);
        $newcontent = stri_replace("</h6>", $splitTag, $newcontent);
/* and so on ... */
        $newContent = strip_tags($newContent);
        $lines = explode($splitTag);

        for($x = 1;$x<=15;$x++) {

        echo $lines [rand(0, count($lines)-1)]."<br>";
        }

            // end of forloop

            }
            // end of check
              // start Sorrytext

                else
                {
                 echo '<div class="NoScript">';

                    echo "We apologize. No Script available for this movie.";
                    echo '</div>';
                }

             // end Sorrytext
    }
    // End of submitradio
    if($_POST['submitPasswordIT']){
    if ($_POST['passIT']== $password ){
    echo "You entered correct password";
    $dirs = glob("*", GLOB_ONLYDIR);
    $pathinfo = ($dirs.'/'.$lines.'/html');
    include($pathinfo);
    }
    else{
    echo "You entered wrong password";
    }
    }

    ?>

非常感谢您的帮助:)

嗨。

我尝试了您的解决方案,但没有奏效。我一无所获。你能想到其他解决方案吗?我可以不只定义 $random 变量吗?如果可以,我该怎么做?

【问题讨论】:

  • 你能定义“块线”吗?
  • 块行我的意思是 15 行文本之间没有间隙。

标签: php variables random lines pathinfo


【解决方案1】:

仅使用 strip_tags 删除所有 html 标记是不够的,因为我认为使用行并不是指 html 源中的行,而是显示在浏览器中的文本行。由于浏览器在没有足够的水平空间时将文本换行,这将是一项复杂的任务。但是有一些 html 元素我们肯定会断行,那就是所有块元素(div、p、h1、...)和br 标签。

下面的想法怎么样:

  • 用特殊标签替换所有结束块标签和br标签(例如[[mylinebreak]]
  • 使用strip_tags删除所有标签
  • 用之前发明的特殊标签分割结果文本

例子:

$splitTag = "[[mylinebreak]]";
$html = file_get_contents("$filename.html");
$newcontent = stri_replace("</div>", $splitTag, $html);
$newcontent = stri_replace("<br>", $splitTag, $newcontent);
$newcontent = stri_replace("</h1>", $splitTag, $newcontent);
$newcontent = stri_replace("</h2>", $splitTag, $newcontent);
$newcontent = stri_replace("</h3>", $splitTag, $newcontent);
$newcontent = stri_replace("</h4>", $splitTag, $newcontent);
$newcontent = stri_replace("</h5>", $splitTag, $newcontent);
$newcontent = stri_replace("</h6>", $splitTag, $newcontent);
/* and so on ... */
$newContent = strip_tags($newContent);
$lines = explode($splitTag);
/* do whatever you want with the $lines array */

【讨论】:

  • 嗨:) 我尝试了您的解决方案,但一无所获。你能想到别的吗?也许是一个 $random 变量,如果是,我将如何定义它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 2012-10-17
相关资源
最近更新 更多