在文本区域键入以下内容时:
<div>
提交后,应将其存储为
<div>
在DB中。我怎样才能做到这一点?
要对HTML进行编码,请使用htmlentities
,然后使用html_entity_decode
再次对其进行解码。
编码
<?php
$str = "<div>";
echo htmlentities($str);
?>
输出:
<div>
解码
echo htmlentities($str, ENT_QUOTES);
输出:
<div>
http://php.net/manual/en/function.htmlentities.php
<?php
$str = "A 'quote' is <b>bold</b>";
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str, ENT_QUOTES);
?>