SoFunction
Updated on 2025-03-02

ThinkPHP automatically escapes the solution to the read error caused by storing rich text editor content

There is a configuration option in the conf file of ThinkPHP

'DEFAULT_FILTER'        =>  'htmlspecialchars', // Default parameter filtering method is used in I functions...
This method is enabled by default. In other words, the data we store in the database will be escaped by the htmlspecialchars function.

I used the Kindeditor rich text editor in my project (maybe you are using Ueditor\ckeditor), edit the content of the article through the rich text editor and store it in the database, and then read it from the database and display it on the web page.

In this process, I encountered a problem. When I directly display the text data I submitted to the database in the insert method, it can be parsed into an HTML page normally, but when I read the HTML data from the database, the content of the article becomes a whole piece of HTML code. dump outputs the array content I read, and it can be clearly seen that it has been escaped.

1. Content stored in the database and examples of the article content read out:

<p>
 Do you know?
</p>

2. The content parsed in the html page:

“<p>
 <span style="white-space:pre"> </span><img src="/17joys/Public/kindeditor/attached/image/20140807/20140807110915_31727.jpg" alt="" width="800" height="600">
<span style="white-space:pre"> </span></p>”

It is obvious that the content that was supposed to be an HTML tag is now parsed into the content in the text, and double quotes are added to the outermost part of the text content.

The normal display should be without double quotes. The data read out from the database is the source code of the HTML page. After inserting the HTML page, the tag is interpreted and the content in the tag is displayed as a page.

Through the above analysis, it can be confirmed that ThinkPHP automatically escaped the content during the content storage process, resulting in the content stored in the database becoming escaped content. When read out again, HTML will automatically escape the content originally belonging to the tag into the main text.

Therefore, decisively search for the configuration file in the conf directory of TP, disable the DEFAULT_FILTER function, and call it manually when you need to use it in the future.

Tested again, this time the problem was solved.