2015-01-28 8 views
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 변수를 정의 할 수는 없습니까? 그렇다면 어떻게해야합니까?

+0

"블록 라인"을 정의 할 수 있습니까? –

+0

블럭 라인이란 15 개의 라인 사이에 간격없이 텍스트를 의미합니다. – userSC

답변

0

strip_tags으로 모든 html 태그를 제거하는 것만으로는 충분하지 않습니다. 행을 사용하면 HTML 소스에서 행을 의미하지는 않지만 브라우저에 텍스트 행을 표시한다고 가정하기 때문에 충분하지 않습니다. 가로 공간이 충분하지 않으면 브라우저가 새 줄에 텍스트를 래핑하므로 복잡한 작업이됩니다. 그러나 블록 요소 (div, p, h1, ...)와 br 태그가 모두 끊어지는 것을 확실히 알고있는 html 요소가 있습니다.

방법은 다음과 아이디어에 대해 모든 닫는 blocktags 교체

  • strip_tags
  • 를 사용하여 특수 태그 (예 [[mylinebreak]])
  • 제거 모든 태그와 br 태그 이전에 생성 된 텍스트를 분할 발명 된 특수 태그

예 :

$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 */ 
+0

안녕하세요 :) 당신의 해결책을 시도했지만 아무것도 얻지 못했습니다. 다른 생각을 할 수 있을까요? 어쩌면 $ 임의의 변수와 예, 내가 정의 할 수 있을까요? – userSC