Jump to content

Parsing data for custom tags


supertrucker

Recommended Posts

I'd like to be able to let my forum users use custom tags, like:

Can [b]Billy[/b] come out and play

Is there an easy way to parse user data that was input to see if they included beginning and ending custom tags, so that I can wrap it in the appropriate HTML bold tags? I don't want to let them go crazy in my forums or chat rooms with direct HTML.Thanks,ST

Link to comment
Share on other sites

I'm not sure if you're after a replacement algorithm (easy) or a validation and replacement algorithm (not so easy). I mean, a global replacement of "<b>" for "" just requires a call to str_replace. And you can simplify the task of replacing lots of tags if you use arrays. But if you first want to validate that every opening tag has a correct closing tag, and then do something different if the user goofed--well, it's more complicated.So what exactly are you after?

Link to comment
Share on other sites

Well, in my forums and chat rooms, all HTML and PHP data is stripped using Strip_tags. So I have to devise a simple way for users to format text, i.e., bold, italic, some form of quoting, links, etc. If I just replaced for <b> then if they didn't close the tag, it could affect every other line that is queried from my db into the users browsers.So, I'm assuming I'm after the more complicated method. I did do a simple experiment playing with the XML functions, but it was very simple, and am not sure how I'd actually incorporate it into my project :)ST

Link to comment
Share on other sites

So, I'm assuming I'm after the more complicated method. I did do a simple experiment playing with the XML functions, but it was very simple, and am not sure how I'd actually incorporate it into my project :)
You know, I've wondered myself how to do that in a simpler fashion (I know the complicated regex routine, but it's very error prone), and now that you've putted it that way, I think I know the answer. Unfortunatly, the answer is not as simplistic as I'd want, but it's really simple for my kind of taste (I'm an XML fan, see), and is far less error prone:1. Create an XML Schema that declares the allowed XHTML elements in your output (you should probably use the W3C XHTML schema as a base).2. Create a literal translation of to <b> and other similar BBCodes you may want.3. Validate the resulting (possibly invalid and/or malicious) document against the schema. If invalid, return an error, or (better yet) reattempt parsing, this time as HTML (this will take care of non well formed inputs), validate against the schema again (to avoid malicious <scirpt> inputs created after HTML parsing). If valid in either validation, accept the input.This first step is the hardest step. Once you're passed that, it's a breeze.
Link to comment
Share on other sites

I'm bookmarking this topic, so I can keep your outline, otherwise I'll forget it, thank you!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...