I have an old custom built content management system in PHP, where users can write articles.
When the body text is displayed, I do a bunch of str_replace and ereg_replace calls to parse links and headings and other bits.
Now, there is a need to be able to enter a block of HTML code within the body text (occasionally) and have it not-parsed by the before-mentioned functions. These blocks should actually be rendered as HTML.
The block could be surrounded in a tag (i.e. [html] etc) to identify it but how do I exclude parsing of this section when I’m basically just running the before-mentioned functions over the entire body text block?
Obviously you need to parse something that does not include the area you want to leave as-is. So here’s one way you might do that:
For each
[html]block, generate a unique identifier (e.g. throughuniqidor hashing something, possibly the contents of the block). Check that this identifier (which should be something that does not get affected by your other parsing, so alphanum would be a good choice) does not actually exist in the text. If it does, generate another.Save the identifier and the contents of the corresponding
[html]block in an array using the identifier as a key. Then replace the substring[html]blah[/html]with[html]identifier[/html]in your input.Proceed with parsing as normal, and when you are done replace back the
[html]identifier[/html]bits with the real content. You can do this with just one call topreg_replacefor all blocks if you take advantage of the/e(evaluate replacement string as code) option.