Skip to content
Advertisement

How to preserve formatting and protect the code from scripts

I am trying to find a way to escape the scripts or tags inserted to my database and at the same time preserve the text formatting. However I bumped into several problems that only solved either of the two. I’m trying to do it in $blog['title'] and $blog['content']

I tried using nl2br() to preseve the formatting. It works on preserving the formatting but it still does not escape HTML tags and scripts.

These are my codes. Please note that I am using class where all my functions are there and instantiated the class in my current blog page.

Blog Post HTML Block

JavaScript

I have created a function I called escape in my php class

JavaScript

if I use this:

JavaScript

it outputs: output1

but if I remove the escape function like this

JavaScript

it outputs: (preserved formatting but also executes the script) alert enter image description here

Advertisement

Answer

Call it the other way round:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement