SoFunction
Updated on 2025-04-04

Detailed explanation of ThinkPHP's method of using Ueditor

This article describes the method of ThinkPHP using Ueditor. Share it for your reference, as follows:

I believe many people are still using FCkeditor. I used to use it, but later I found out that Ueditor is better than Fckeditor and is much better in operation than Fckeditor, so I still tried to use ueditor. However, I still encountered some problems using ueditor under the ThinkPHP framework and spent some time solving these problems. In this way, I will write a tutorial on using ueditor under ThinkPHP for those who need to use it.

1. Create a "Public" folder in the root directory of the website. This folder can be replaced by __PUBLIC__ in ThinkPHP, so it is convenient for our program to write, and then put the ueditor file into this folder.

2. Import the js and css that ueditor wants to use in the template file where we need to use ueditor. For example, I added the following code to App/Tpl/default/Venter/:

<script type="text/javascript" src="__PUBLIC__/ueditor/editor_config.js"></script>
<script type="text/javascript" src="__PUBLIC__/ueditor/editor_all.js"></script>
<link rel="stylesheet" href="__PUBLIC__/ueditor/themes/default/"/>

3. Configure the location of Ueditor relative to the root directory of the website. Here I recommend using an absolute path, that is, a path similar to http://localhost:1080, because in the development of website projects, ueditor is used in many places, but due to the different locations used, the relative paths of the current directory and the server root directory change, which makes ueditor unusable. For example, the path I configured (config file: editor_config.js in the ueditor directory) will replace the following code:

URL= (0,("\/")+1).replace("_examples/","");
//Here you can configure the relative or absolute path of the ueditor directory in your website (referring to the absolute path starting with http)

Replace with:

URL = "http://localhost:1080/Public/ueditor/";

4. The following is an application for the use of forms:

&lt;form action="__ROOT__//Venter/ventChk" method="post"&gt;
  &lt;script type="text/plain"  style="width:500px;"&gt;&lt;/script&gt;
  &lt;input type="checkbox" name="opened" value="1"/&gt;Publicly published
  &lt;input type="submit" value="vent" /&gt;
&lt;/form&gt;
&lt;!--Register for Baiduueditor--&gt;
&lt;script type="text/javascript"&gt;
  var editor = new ({
    toolbars:[['Spechars','Emotion','InsertImage','Bold','Italic','ForeColor','FontFamily','FontSize', ]],//Customize ueditor toolbar    initialContent: '&lt;span style="color:#ccc">Click here to vent</span>',    minFrameHeight: 100,//Initialize the frame size    autoFloatEnabled: false,//The toolbar automatically floats -》Close    textarea:'myVent'//Form form will obtain the data submitted by the form through this parameter  });
  ("Vent");
&lt;/script&gt;
&lt;!--BaiduUeditorRegistration is complete--&gt;

As shown in the above code, the contents of the Ueditor editing area can be obtained through $_POST['myVent'].

5. How to use it in Thinkphp? First, we need to submit the obtained content and escape it. Here we use the following code:

$content = htmlspecialchars(stripslashes(POST['myVent']));

The following operation is to insert $content into the database (not talk about it more here).

6. Extract data from the data and escape it so that the content can be displayed normally:

$vent = new VenterModel;
$list = $vent->select();
$this->assign("list",$list)

In this way, the content can be displayed in the corresponding html template:

<volist name="list" >
{$|htmlspecialchars_decode}
</volist>

Here is a usage of thinkphp template, that is, you can use functions in the template. Here is the function htmlspecialchars_decode to escape $vo and content. The above code can be seen as this:

I used this method for a long time to choose this method here. I didn't consider this method at that time. At that time, I just thought that since the value passed by the htmlspecialchars_decode function cannot be an array, the data extracted from the database cannot be passed directly to him. Later, I thought that by traversing the array, I could make every element in the array htmlspecialchars_decode. Although the content can be escaped, it cannot be displayed in the template. I didn't think of it until I finally read some official manuals. Therefore, when doing programs, you must read more official documents, so many problems can be solved.

PS: Here are a few formatting/beautification/conversion tools for this site that can help you organize the messy code. I believe you can use it in future development:

PHP code online formatting and beautification tool:
http://tools./code/phpformat

JavaScript code beautification/compression/formatting/encryption tools:
http://tools./code/jscompress

Online XML formatting/compression tools:
http://tools./code/xmlformat

JSON code formatting and beautification tool:
http://tools./code/json

Online XML/JSON mutual conversion tool:
http://tools./code/xmljson

json code online formatting/beautification/compression/editing/converting tools:
http://tools./code/jsoncodeformat

sql code online formatting and beautification tool:
http://tools./code/sqlcodeformat

For more information about thinkPHP, please visit the special topic of this site:ThinkPHP Introduction Tutorial》、《Summary of common methods of ThinkPHP》、《Summary of the usage of cookies in PHP》、《Basic tutorial on getting started with smarty templates"and"PHP template technical summary》。

I hope that the description in this article will be helpful to everyone's PHP programming based on the ThinkPHP framework.