如何在提交后替换字符串

When typing in textarea this:

<div>

After submit this should be stored as

&lt;div&gt;

in the DB. How can i achieve this?

To encode HTML use htmlentities and use html_entity_decode to decode it again.

Encode

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

Output:

&lt;div&gt;

Decode

echo htmlentities($str, ENT_QUOTES);

Output:

<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);
?>