回显15个随机的html文件行

时间:2015-01-15 10:03:12

标签: php

我必须为大学做一个php任务。

如果在同一个文件夹中有电影脚本文本和相同的图像,我如何回显html文件的15个随机行?

到目前为止我尝试过:

$folder = 'Filmnoir/';
$hitchcock ='Hitchcock/';
$shakespear ='Shakespear/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$file2 = glob ($hitchcock.$filetype);
$file3 = glob ($shakespear.$filetype);
$count = count($files);
$count1 = count($file2);
$count2 = count($file3);
$scripttype ='*.html';
$Scripts = glob($ScriptFilmNoir.$scripttype);
$Scripts=file_get_contents("DoubleIndemnity.html");

if($_POST['radio1']=="0"){
if(($i %1)==0){
for ($i = 0; $i < $count; $i++)
{

    echo '<div class="image2">';
    echo '<img src="'.$files[$i].'" />';
    echo '</div>';
    if (condition) {
    include 'DoubleIndemnity.html'; }
    echo '</td></tr>';
}

echo '<div class="Passwordtext">';
echo 'Type in password to view full Script';
echo '</div>';
echo "<label><div class=\"password\"><input type='password' name='code' value='code'/></div></label>";
echo "<form method='POST' action='ca1_result.php'>";
echo "<div class=\"SubmitIt\"><input type='submit' name='submitScript' value='Submit Password'/></div>";
echo '</form>';
}
}
if($_POST['radio1']=="1"){
echo '<div class="Sorrytext">';
echo 'We apologize. No script available for this movie.';
echo '</div>';
}
if($_POST['radio1']=="2"){
echo '<div class="Sorrytext">';
echo 'We apologize. No script available for this movie.';
echo '</div>';
}

我希望在同一个文件夹中有一个图像(例如DoubleIndemnity.png)和电影脚本(例如DoubleIndemnity.html)时实现这一点,我可以获得15个电影脚本文本的随机行和图像。我可以使用glob函数吗?如果是,我将如何实现?

我可以问另一件事吗?当我提交密码时如何获得完整的电影脚本?

我试过了:

foreach($files as $file2) {
if($_POST['submitPassword']){
if($file2 === '.' OR $file2 === '..' OR $file2 === 'thumbs.db' OR !is_dir($folder.'/'.$file2)) {continue;}
    if(file_exists($folder.'/'.$file2.'/doubleindemnity.gif') AND file_exists($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm')) {
        echo '<div class="Container">';
        echo "<div class='image2'><img src='$folder/$file/doubleindemnity.gif'>";
        $lines4 = file($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm');
         $count = count($lines4);
        for($a = 0;$a < $count;$a++) {
        echo substr($lines4[$a],strlen($folder),strpos($lines4[$a], '.')-strlen($folder));
        }
        echo "</div>";
    }
      echo "</div>";
}
}
?>

使用该代码我只需从html文件中获取几行代码。我不想要那个。我想要全文。我怎样才能使用字符串替换函数来删除代码并从段落中接收文本?

干杯:)

3 个答案:

答案 0 :(得分:0)

这段代码将循环通过预定义文件夹中的文件夹(例如:/ movies)。如果内部的任何内容是文件夹,并且该文件夹有2个文件:image.png和lines.html,它将会显示。如果它有这两个文件,它首先放置图像,然后它将读取HTML文件,并从该文件中写下15条随机行。

<?php
$folder = "movies";
$files = scandir($folder);
foreach($files as $file) {
    if($file === '.' OR $file === '..' OR $file === 'thumbs.db' OR !is_dir($folder.'/'.$file)) {continue;}
    if(file_exists($folder.'/'.$file.'/image.png') AND file_exists($folder.'/'.$file.'/lines.html')) {
        echo "<div class='image2'><img src='$folder/$file/image.png'>";
        $lines = file($folder.'/'.$file.'/lines.html');
        for($x = 1;$x<=15;$x++) {
            echo $lines[rand(0, count($lines)-1)]."<br>";
        }
        echo "</div>";
    }
}

答案 1 :(得分:0)

循环遍历$directory中的文件,然后将15行随机行输出到html变量中,如果图像存在,则将其添加到图像html变量中。如果它们都被添加,则返回结果。 (未经测试但应该有效)

<?php
//I dont know where you have got some of these variables from
$directory = "Filmnoir";
$ScriptFilmNoir = "DoubleIndemnity";
$scripttype ='html';
$supportedImages = Array("png", "jpg", "jpeg");
$html = "";
$imagehtml = "";
$allFiles = scandir($folder);
$imageFound = 0;
$htmlFound = 0;

foreach($files as $file) {
    if($file === '.' || $file === '..' || $file === 'thumbs.db' || is_dir($file)) {
        continue;
    }
    $nameWithoutExtension = substr($file, 0 , (strrpos($file, ".")));
    $fileExtension = $id = substr($file, strrpos($file, '.') + 1);
    if($nameWithoutExtension == $ScriptFilmNoir){
        if($fileExtension == $scripttype){
            $htmlFound = 1;
            $lines = file($directory . "/" . $ScriptFilmNoir . "." . $scripttype);//file in to an array         
            $range = range(1, count($lines));
            $range = array_flip($range);
            $range = array_rand($range, 15);

            foreach($range as $line){
                $html .= $lines[$value] . PHP_EOL;
            }
        }elseif(in_array ( $fileExtension , $supportedImages)){
            $imageFound = 1;
            $imagehtml = "<img src='{$directory}/{$ScriptFilmNoir}.{$fileExtension}' /><br />";
        }
    }
}
if($imageFound == 1 && $htmlFound == 1){
    echo $imagehtml . $html;
}

答案 2 :(得分:0)

我认为欧文正在寻找的是随机提取15行而不是15行。

使用@ ThijmenDF的示例,只需更改

即可
for($x = 1;$x<=15;$x++) {
    echo $lines[rand(0, count($lines)-1)]."<br>";
}

$random = rand(0, count($lines)-1);
for($x = 1;$x<=15;$x++) {
    echo $lines[$random + $x]."<br/>";
}