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?
Update
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...
NULL
and have each post reconverted on next access?addslashes
! Use query parameters so they're unnecessary and you don't risk sql injection!