views:

155

answers:

3

I have created a very simple markup parser in php. However, it currently uses str_replace to switch between markup and html. How can I make a "code" box of sorts (will eventually use GeSHI) that has the contents untouched?

Right now, the following markup: [code][b]Some bold text[/b][/code] winds up parsing as the code box with <b>Some bold text</b>.

I need some advice, which option is best?

  • Have it check each word individually, and if it is not inside a [code] box it should parse
  • Leave it as is, let users be unable to post markup inside of [code].
  • Create another type of code box specifically for HTML markup, have [code] autorevert any < or > to [ and ].

Is there maybe even another option? This is a bit tougher than I thought it would be...

EDIT: Is it even worth adding a code box type thing to this parser? I mean, I see how it could be useful, but it is a rather large amount of effort for a small result.

+1  A: 

You could break it down into multiple strings for the purposes of using the str_replace. Split the strings on the [code] and [/code] tags - saving the code box in a separate string. Make note of where it went in the original string somehow. Then use str_replace on the original string and do whatever parsing you like on the code box string. Finally reinsert the parsed code boxes and display.

Just a word of warning though, turning input into html for display strikes me as inherently dangerous. I'd recommend a large amount of input sanitization and checking before converting to html for redisplay.

Daniel Bingham
It does not convert everything to html, only a specific array. And < and > are converted to their html entities ;)
Cyclone
And yeah, I am thinking that is what I will do.
Cyclone
+1  A: 

Why would you reinvent the wheel?

There's plenty of markup parsers already.

Anyway, just str_replace won't help much. You'd have to learn regular expressions and as they say, now you've got two problems ;)

metrobalderas
I like to try and build things myself if possible, plus I am nearly done anyway. Actually, str_replace works until we hit the code box speed bump.
Cyclone
A: 

HTML beautifier is pretty sweet. http://pear.php.net/package/PHP_Beautifier . The have a decorator class as well that would probably suit your needs.

easement
Does the decorator class handle things like my markup tags?
Cyclone