Search code examples
phphtmltagsposting

Text appears with tags


So what's the problem with this code? $content has text with html tags, but, when I post something, tags are not working and showing up as plain text.

    <?php
        require_once("nbbc/nbbc.php");
        $bbcode = new BBCode;
        $sql = "SELECT * FROM posts ORDER BY id DESC";
        $res = mysqli_query($db, $sql) or die(mysqli_connect_error());
        $posts = "";
        if (mysqli_num_rows($res) > 0){
            while($row = mysqli_fetch_assoc($res)) {
                $id = $row['id'];
                $title = $row['title'];
                $content = $row['content'];
                $date = $row['date'];

                $output = $bbcode->Parse($content);
                    if (strlen($output) > 1000) {
                        $stringCut = substr($output, 0, 1000);
                        $output = substr($stringCut, 0, strrpos($stringCut, ' '))." ... <a href='view_post.php?pid=$id'>Lasīt Vairāk</a>"; 
                    }
                $posts .="<div><h1 style='margin-left:0'><a href='view_post.php?pid=$id'>$title</a></h1><p style='margin:3px; font-style:italic; opacity: 0.6;'>$date<p><p>$output</p></div><hr>";
            }
            echo $posts;
        } else {
            echo "Oops, no new posts";
        }

    ?>

Solution

  • If the $content is stored as encoded, ie tags are translated to &lt; and &gt; then use PHP native function html_entity_decode($content) to decode it to real < and > characters.

    html_entity_decode — Convert all HTML entities to their applicable characters