2012-08-02 31 views
1

我爲流行硬件站點的用戶創建了幾個PHP文件,用於「Metro」他們的新聞帖子。它工作得很好,你添加文章的標題,鏈接等,然後它吐出來的地鐵瓷磚格式。如何使用PHP來顯示HTML源代碼

請看:http://briandempsey.org.uk/Newstool/index.php

當用戶提交,它使用提供創造職位的信息。現在,我需要以某種方式使用PHP或其他語言來顯示它下面生成的代碼,以便用戶可以複製和粘貼它。我不知道如何做到這一點。

回答

0

由於您使用GET方法傳遞表單數據,你可以代替它傳遞給創建一個URL來拉從HTML頁面...

的index.php會具有上面顯示的形式,並將發佈到urlCreator.php。

form.php可以被刪除,因爲它不再需要,魔術將發生在urlCreator.php文件中。

urlCreator.php(NEW)將代碼它像這樣:

<?php 
    // urlCreator.php will get variables passed to it from index.php 

    // Now create the url using the passed variables 
    $url = "http://briandempsey.org.uk/Newstool/form.php?title=" . $_GET['title'] . "&color=" . $_GET['color'] . "&Articlecontent=" . $_GET['Articlecontent'] //and so on to build the whole url 

    //Now get the pages html 
    $html = file_get_contents($url); 
?> 

現在你已經在你可以使用str_replace函數清理或操縱它的變量的HTML,但是你會喜歡。

<?php 
    // Still in urlCreator.php 

    // With the html in a variable you could now edit out the divs and echo the results 
    $html = str_replace("<div style=\"background-color: #008299; height: auto; width: 100%; margin-left: 10px;\">", "", $html); //removes the intro div 
    $html = str_replace("</div>", "", $html); //removes the closing div 

    //Finally echo out the html to the page for copy+paste purposes 
    echo $html; 
?> 
6
header('Content-Type: text/plain'); 
+0

謝謝,但我注意到另一個錯誤。如果用戶沒有添加5個源,它仍然顯示DIV。有沒有解決這個問題的快速解決方法? – Joshua 2012-08-02 15:04:45

+0

無恥的自我顛簸,我可以用一些Jscript來實現嗎? – Joshua 2012-08-02 15:52:00

+0

不能在'text/plain'模式下使用javascript或HTML - 它只是在瀏覽器的結尾沒有任何後期處理的情況下輸出。你只需要過濾額外的div,或者使用完全不同的解決方案。 – 2012-08-02 20:05:14