2012-08-02 90 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