Long story short, I have a social networking site I wrote a while back as a "proof of concept" to myself that I had the skills to do it. I decided to revisit it, and "improve" a lot of the bad coding practices I had when I first wrote the thing (3 years ago).

My current situation

Users can create posts. When posting, some bbcode-style tags are allowed, as well as lots of smileys and such. Currently, I take the post, and store it in a mySQL database just the way it was written. When retrieving the post, I use php & regex expressions to convert these tags and smileys in to the appropriate html. For example, I might convert :) in to <img src='/images/smiley.gif' />.

It works well... but then I got to thinking (probably a bad idea).

My proposed solution

I got to thinking that doing this conversion every single time the post is retrieved from the database is probably not optimal, as there is an amount of server side CPU usage to perform the all the regex substitutions.

The approach I think I would like to take is doing the conversion ahead of time, and storing the html version of the post in the database. This way, when I retrieve it the next 100 times or more, I don't need to keep doing the conversion. I plan on storing both the original text (to be used for editing the post) as well as the converted text (for displaying the post).

Other than taking up more space in the database (I'm now storing a raw text version as well as an HTML version, is there any downside to the approach I'm thinking of using? Or does this sound like a reasonable optimization of the whole process?


So... after reading the comments from you guys, it sounds like there's probably not a lot of reason (okay, no reason) to try and do this client side rather than server side as I'm doing it now. Why did I come up with such a crazy idea in the first place?

Well... right now, I store bbcode-ish text in the database, and when creating the page, run a function replacing all that code with html. What I want to do is show a preview box, similar to the StackExchange site, so that when the users are entering their posts, they can see what the formatted result would be. I assume that I need to do this via Javascript, so I've basically duplicated the preg_replace I use in php with replace functions in Javascript, so I can show the preview as the user enters their text.

Soooo... I'm now duplicating the text replacement code, once in php, once in js. I figured maybe I should find a way to not have to write the code twice... but after some thought, maybe it's a small trade off to handle it this way.

I just wanted to share what's rolling around in my head to put my on this path in the first place...

  • This seems more fitting for Programmers. Would you like it migrated?
    – Jamal
    Dec 9, 2013 at 21:39
  • Yes, that would be good, thanks... I just setup an account on Programmers since I hadn't visited that SE site before :) Thanks!
    – Charlie74
    Dec 9, 2013 at 21:43
  • 1
    @Matt as he's also storing the original, conversions can be redone if necessary - though that might take some processing... Perhaps set the converted data column to NULL and have each post reconverted on next access?
    – just me
    Dec 9, 2013 at 23:07
  • 1
    @Charlie74 E-gads, don't use addslashes! Use query parameters so they're unnecessary and you don't risk sql injection!
    – Izkata
    Dec 10, 2013 at 1:37
  • 1
    @Izkata sadly, back when I wrote the site, I was using mySQL statements for everything. The site has all kinds of vulnerabilities, I would imagine... though learning PDO is on my to-do list to go through and fix that stuff :)
    – Charlie74
    Dec 10, 2013 at 2:36

1 Answer 1


So long as you're keeping the original for the case when something in the rendering of the bbcode changes down the road, and the extra space isn't an issue for you, there's nothing bad about that approach. You're effectively implementing caching. If you go this route, make sure you are using css rather than hard coded styles, this way you can adjust to the client accordingly without changing the pregenerated html.

Another option is to send the bbcode to the browser in its original form, and have client side javascript handle the text transformation.

  • Interesting... your last comment send the bbcode to the browser in its original form, I had considered this as a possibility, and then got stuck trying to figure out how this might work. If the page is rendered dynamically in php, it seems I would want to have the text rendered before the page loads. If I'm not mistaken, would attempting this other option cause the page to initially load showing BBCode, and then "update" once the Javascript had time to go through and edit all the posts?
    – Charlie74
    Dec 9, 2013 at 23:11
  • How you handle it client side would depend how you're sending it back. If its a PHP page generating output, you could hide the container block that holds the bbencoded text until the content was processed. Or you can embed the encoded text as JSON in the document, and render it entirely in javascript (for ex, if you have multiple pieces of text to render, such as comments). But, keep in mind, unless you have a good deal of traffic to begin with, you'll never notice the extra processing required to render bbcode the 'old fasioned' way on the server when the page is generated. Dec 10, 2013 at 0:14
  • The more I read the comments from yourself and from @Matt, it seems that I am trying to fix something that isn't actually broken. I definitely don't have the traffic to warrant all this code change. I'll update the original post here with the why I thought of this crazy change in the first place... though I think maybe it will be best to leave well enough alone for now.
    – Charlie74
    Dec 10, 2013 at 2:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.