提交后如何替换字符串


how to replace string after submit

在文本区域键入以下内容时:

<div>

提交后,应将其存储为

&lt;div&gt;

在DB中。我怎样才能做到这一点?

要对HTML进行编码,请使用htmlentities,然后使用html_entity_decode再次对其进行解码。

编码

<?php
$str = "<div>";
echo htmlentities($str);
?>

输出:

&lt;div&gt;

解码

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 &lt;b&gt;bold&lt;/b&gt;
    echo htmlentities($str);
    // Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
    echo htmlentities($str, ENT_QUOTES);
?>