Question

I want to create BBCode kind of tags for my custom Blog system. However I have no Idea where to start from. I'm not asking for the full working script or anything (if it's a long code whitch it probably is), but I want to know what do I need and some basic leads to get me going. Thanks for all the hints!

This is the code that I'm wanting to add it to:

    $query=$db->prepare("SELECT post_id, title, LEFT(body, 400) AS body, category FROM posts INNER JOIN categories ON categories.category_id=posts.category_id ORDER BY post_id DESC LIMIT $start, $per_page");
        $query->execute();
        $query->bind_result($post_id, $title, $body, $category);    
while($query->fetch()):
        $lastspace = strrpos($body, ' ');?>
    <article>
    <div class="5pxPadding">
    <h2><?php echo "<a href='post.php?id=$post_id'>$title</a>";?></h2>
    <?php echo "Category: ", $category;?>
    <?php echo "<br><br>";?>
    <p><?php
//END BBCODE, ECHO POST
    $body_sub = substr($body, 0) . "<br><br><a href='post.php?id=$post_id'> Continue Reading →</a>";
    echo nl2br($body_sub); ?></p>
    <?php echo "<hr>" ; ?>
    </div>
</article>

I know the code is messy btw, you don't need to tell me that.

Was it helpful?

Solution

You could probably do it kind of like this:

$str = 'this is a string with [bold]bold[/bold] text';
$str = preg_replace('/(\[bold\])/', '<strong>', $str );
$str = preg_replace('/(\[\/bold\])/', '</strong>', $str );
echo $str;

I'm not great at regex, so I'm sure some wizard can combine my two statements into one, but this is the general idea. Just replace any bbcode with the proper html?

DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top