在MySQL中存储HTML

GST*_*TAR 4 php mysql html-encode

我将HTML和文本数据以原始格式存储在我的数据库表中 - 但是我在使其正确输出时遇到了一些问题.以下是存储在表AS IS中的一些示例数据:

<p>Professional Freelance PHP & MySQL developer based in Manchester.
<br />Providing an unbeatable service at a competitive price.</p>
Run Code Online (Sandbox Code Playgroud)

要输出这些数据我做:

echo $row['details'];
Run Code Online (Sandbox Code Playgroud)

这会正确输出数据,但是当我执行W3C验证器检查时它会说:

character "&" is the first character of a delimiter but occurred as data
Run Code Online (Sandbox Code Playgroud)

所以我尝试使用htmlemtities,htmlspecialchars但这只会导致HMTL标签在页面上输出.

这样做的正确方法是什么?

new*_*bie 10

&amp;而不是&.


pat*_*tyd 5

你想要做的是使用php函数htmlentities()...
它会将你的输入转换为html实体,然后在输出时它将被解释为HTML并输出为该HTML的结果...
例如:

$mything = "<b>BOLD & BOLD</b>";
//normally would throw an error if not converted...
//lets convert!!
$mynewthing = htmlentities($mything);
Run Code Online (Sandbox Code Playgroud)

现在,只需插入$mynewthing您的数据库!!