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?
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...