Blog

Hier geht es um Webentwicklung und Nagios Monitoring.

CKEditor converts HTML entities back to angled brackets when coming from a database

Geschrieben am von Klement

While trying out some syntax highlighting plugins for CKEditor (to start this new blog ;) ) I noticed that CKEditor has a problem when you like to display < and > (needed for code examples in HTML).

CKEditor handles angled brackets the right way when you create a new post and automatically converts them to their corresponding HTML entities &lt; and &gt;. But when you save the post to a database and load the content from a database (or JSON, etc.) back to CKEditor, all HTML entities for angled brackets (&lt; and &gt;) are converted back to < and >. So when you like to edit a post all the HTML examples are suddenly empty. 

But there's a workaround for this problem. Convert & to &amp; in your backend before handing over the string to the textarea of CKEditor.

//PHP example
$string = strtr($string, array(
'&lt;' => '&amp;lt;',
'&gt;' => '&amp;gt;',
));

This bug exists for a while in CKEditor and is still not fixed (by the date of this blog post).