如何解析来自数据库的html

时间:2012-01-12 20:50:44

标签: php mysql parsing

从html实体解码回HTML后,如何输出从数据库返回的HTML。我正在学习如何使用Tinymce而且我被卡住了。我无法回显HTML,因为它只是在页面中打印它。我是否必须使用DOMDocument查看DOM端? 我在不同的网站上看过这个问题,但从来没有回答清楚。或许我是一个新手,答案就在我面前。谢谢!

<?php
$page_title = "Brian Aylward comedy website";
$current_page = "home";

include("site_admin/tinymce/shows/db.php");
doDB();

$get_contents_sql = "SELECT * FROM tinymce_contents";
$get_contents_res = mysqli_query($mysqli, $get_contents_sql)
or die(mysqli_error($mysqli));

if ($get_contents_res = mysqli_query($mysqli, $get_contents_sql)) {

while ($row = mysqli_fetch_assoc($get_contents_res)) {

   $contents = $row['contents'];

   $fill_block = html_entity_decode($contents);
  }
}

mysqli_close($mysqli);


include_once'./includes/header.php'; ?>


<span id="mikemouth"></span>
<div id="jacket">

<h2 id="showtitle">LIVE DATES</h2>
<div id="shows">


    <div class="shows_content">

<?php

//I want to output the HTML here but can't use echo $fill_block; since it will print
//the HTML in the webpage when I want it parsed as HTML.Does it make sense?

?>

2 个答案:

答案 0 :(得分:3)

如果您使用htmlentities()将HTML存储在数据库中,那么在标记中打印时需要使用html_entity_decode()。虽然我建议您不要首先使用编码标签存储HTML。

假设您在通过htmlentities()传递后将以下HTML代码存储在数据库中:

<a href="http://hello.com">hello</a>

如果你打印它,你会得到类似的东西:

&lt;a href=&quot;http://hello.com&quot;&gt;hello&lt;/a&gt;

要在TinyMCE中正确使用它,您需要通过html_entity_decode()传递它,这将产生正确的标记。

现在假设您在数据库中有以下内容:

<a href="http://hello.com">hello</a>

它可能会像这样存储:

<a href=\"http://hello.com\">hello</a>

在将其发送到标记之前,您需要使用stripslashes()

答案 1 :(得分:0)

您可以使用文本区域在浏览器上显示html。

相关问题