0

I have a database query that returns the raw HTML for a page, but if I use it on my page, it gets shown as plain text (of course). How would I format it as HTML so that it uses the tags and such.

An example of what I have in my database:

<div class="test">SOME TEXT HERE</div> 

But it is also displayed like that. I would like it to format the text as if it was HTML. So it would just display:

SOME TEXT HERE

But that it would also be in a div with the class: "test"

What would be the best approach to reach this goal?

Im using Twig in the MVC model to render the page. So the page renderer is like this

    public function renderArticle() {
        $twig = new TwigHelper();
        $args['title'] = "Artikel $this->articleId";
        $args['blogHTML'] = BlogController::retrieveBlogHTML($this->articleId);
        echo $twig->render('article.twig', $args);
     }

And the "BlogController::retrieveBlogHTML" goes like this:

    public static function retrieveBlogHTML($id) {
    $db = DatabaseHelper::get();
    $st = $db->prepare("SELECT PageHTML FROM T_Blog WHERE BlogId = :BlogId");
    $st->execute([
        ':BlogId' => $id,
    ]);
    if ($st->errorCode() !== \PDO::ERR_NONE) {
        return null;
    }
    return $st->fetchAll();
}

This means that I will not be able to use JavaScript at this point in time, if that will be the only way to fix the problem i'll have to build a workaround.

So I dont know if I accidently escape too or something along those lines, but im not using any headers.

3
  • How do you retrieve the div ? Ajax ? Server side code ? Commented Apr 8, 2016 at 9:58
  • 1
    That's probably a matter of setting the right content headers, or maybe you accidentally escaped the HTML. You should really give more details and some code of your current process. Commented Apr 8, 2016 at 9:58
  • Im sorry for my late response, but I have updated my main post with some more information on how I retrieve the data. Commented Apr 9, 2016 at 15:00

1 Answer 1

2

You need to escape the html characters (so < becomes &lt; for example).

In javascript you can use the HE library or theres this function, which is generally fine, but doesn't cover all possible cases that the HE library does

var encodedStr = rawStr.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
   return '&#'+i.charCodeAt(0)+';';
});

If your using php you can use htmlentities, other languages will have a similar function either inbuilt or provided via a library.

Sign up to request clarification or add additional context in comments.

2 Comments

Im not using Javascript at this moment to retrieve the data. I updated my main post to show how I retrieve the information. Sorry for the late answer btw.
I thought twig defaulted to autoescaping? In any case you can force html escaping by using {{ blogHTML.PageHTML|e }} or {{ blogHTML.PageHTML|escape }}. See here: twig.sensiolabs.org/doc/filters/escape.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.