如何使用PHP显示HTML源代码
我为一个流行硬件站点的用户创建了一些PHP文件,用于“Metro”他们的新闻帖子。它工作得相当好,你可以添加文章的标题、链接等,然后以Metro-tile格式显示出来 看一看:如何使用PHP显示HTML源代码,php,html,Php,Html,我为一个流行硬件站点的用户创建了一些PHP文件,用于“Metro”他们的新闻帖子。它工作得相当好,你可以添加文章的标题、链接等,然后以Metro-tile格式显示出来 看一看: 当用户提交时,它使用提供的信息创建帖子。现在,我需要以某种方式使用PHP或其他语言来显示它在下面生成的代码,以便用户可以复制和粘贴它。我不知道该怎么做。因为您正在使用GET方法传递表单数据,所以可以将其传递到一个页面,该页面创建一个url以从中提取html header('Content-Type: text/plain
当用户提交时,它使用提供的信息创建帖子。现在,我需要以某种方式使用PHP或其他语言来显示它在下面生成的代码,以便用户可以复制和粘贴它。我不知道该怎么做。因为您正在使用GET方法传递表单数据,所以可以将其传递到一个页面,该页面创建一个url以从中提取html
header('Content-Type: text/plain');
index.php将具有如上所示的表单,并将发布到urlCreator.php
form.php可以删除,因为不再需要它,神奇的事情将发生在urlCreator.php文件中
urlCreator.php(新)中的代码如下:
<?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);
?>
既然变量中有了html,就可以使用str_替换或随意操作它来清理它
<?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;
?>
由于您使用GET方法传递表单数据,因此可以将其传递到创建url以从中提取html的页面 index.php将具有如上所示的表单,并将发布到urlCreator.php form.php可以删除,因为不再需要它,神奇的事情将发生在urlCreator.php文件中 urlCreator.php(新)中的代码如下:
<?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);
?>
既然变量中有了html,就可以使用str_替换或随意操作它来清理它
<?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;
?>
谢谢,但我注意到另一个bug。如果用户没有添加5个源,它仍然显示DIV。有没有一种快速的方法可以用javascript或其他东西来解决这个问题?不知羞耻的自我碰撞,我可以用一些Jscript来完成吗?不能在text/plain
模式下使用javascript或HTML-它只是在浏览器端输出,而无需任何后期处理。你只需要过滤额外的div,或者使用完全不同的解决方案。谢谢,但是我注意到了另一个bug。如果用户没有添加5个源,它仍然显示DIV。有没有一种快速的方法可以用javascript或其他东西来解决这个问题?不知羞耻的自我碰撞,我可以用一些Jscript来完成吗?不能在text/plain
模式下使用javascript或HTML-它只是在浏览器端输出,而无需任何后期处理。您只需过滤额外的div,或者使用完全不同的解决方案。