0

我在一个 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 变量吗?如果可以,我该怎么做?

4

1 回答 1

0

Removing all html tags with strip_tags alone will not suffice, because I suppose that with lines you don't mean the rows in the source of the html, but the text lines displayed in a browser. Since a browser wraps a text in a new line when there is not enough horizontal space, that would be a complicated task. But there are some html elements where we know for sure a line will break, that is all blockelements (div, p, h1, ...) and the br tag.

How about the following idea:

  • replace all closing blocktags and the br tag with a special tag (e.g. [[mylinebreak]])
  • remove all tags using strip_tags
  • split the resulting text with the previously invented special tag

Example:

$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 */
于 2015-01-28T19:36:02.717 回答