Parse Blog Post for Highlighting - how?


I’m working on my own project trying to create a blog. I want the ability to use bbcode like tags for code snippets I’m blogging about. I already have a highlighting class created. If I send a string to it, I get back the proper highlighted text. That part has been taken care of.

The problem I’m having is parsing the submitted blog post so that text stays text and the content between the bbcode like tags gets converted to the highlighted text and put back into the string.

Here’s an example of a simple blog post:

This is plain old blog text.

Here is an example of echo in php:
//this is php code
echo "hello";

Here is an example of var in javascript:
var Cat = getElementById('Hat');

Here is an example of print in php:
//this is another php code
print "hello";

This is plain old blog text.

The contents of the code box above would essentially be posted via a text area. I’ve tried every preg function. I’ve tried tinyMCE. I’ve tried every combination of the string functions in PHP. I’ve tried the dom functions. I’ve also tried several other classes from around the interweb but I can’t find the perfect fit. It’s easy enough to remove the code snippets from the string and convert them to highlighted text…but how do I get the highlighted text back into the original string where they belong?

Basically, I need to parse the string so the text stays text, the code gets removed and converted to highlighted syntax and put back in the proper place within the original string.

I’ve spent two days trying to brute force a solution. Maybe fresh ideas/minds can suggest what I’m missing. Any suggestions?


How many times have you posted a question on Sitepoint only to find your answer with the next search you perform!?!?

I think I found my solution: