开发者

how to escape special characters in php

开发者 https://www.devze.com 2023-02-08 06:17 出处:网络
I am posting this content in the Editor. > <div class=\"faq\"><ul><li><span class=\"q\">What is a teest?</span>

I am posting this content in the Editor.

> <div class="faq"><ul><li><span class="q">What is a teest?</span> 
> <span class="a">A test.</span><div class="spacer"></div></li> </ul></div>

It is saving in the following format.

<p>&lt;div class=\\\"faq\\\"&gt;&lt;ul&gt;&lt;li&gt;&lt;span class=\\\"q\\\"&gt;What is a teest?&lt;/span&gt; <br />&lt;s开发者_开发技巧pan class=\\\"a\\\"&gt;A test.&lt;/span&gt;&lt;div class=\\\"spacer\\\"&gt;&lt;/div&gt;&lt;/li&gt; &lt;/ul&gt;&lt;/div&gt;</p>

So the output is not showing perfectly. I have added stripslashes and addslashes. Even it doesn't work.

What is the best way to resolve this issue?

Thanks in advance...


That's ugly..

You need stripslashes() twice, strip_tags() and htmlspecialchars_decode(), if you want working HTML code.

$html = '<p>&lt;div class=\\\"faq\\\"&gt;&lt;ul&gt;&lt;li&gt;&lt;span> ...';
$html = htmlspecialchars_decode(strip_tags(stripslashes(stripslashes( $html ))));
echo $html;

This will print:

<div class="faq"><ul><li><span class="q">What is a teest?</span> ...
0

精彩评论

暂无评论...
验证码 换一张
取 消