我正在开发一个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变量吗?什么时候可以这样做?
答案 0 :(得分:0)
单独使用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 */