bbcode unparser regex help

前端 未结 3 407
礼貌的吻别
礼貌的吻别 2021-01-06 23:56

I have this function to parse bbcode -> html:

  $this->text = preg_replace(array(
    \'/\\[b\\](.*?)\\[\\/b\\]/ms\', 
    \'/\\[i\\](.*?)\\[\\/i\\]/ms\',         


        
相关标签:
3条回答
  • 2021-01-07 00:05

    Don't.

    Instead, store both the original unparsed text and the processed parsed text. Yes, this doubles the storage requirement, but it also makes it blindingly easy to:

    1. Allow user edits of the original without parsing the BBCode back out
    2. Allow quotes of other user posts, again without parsing
    3. Change the HTML each BBCode generates (just re-parse every post)
    4. Switch BBCode engines down the line (again, just re-parse every post)
    0 讨论(0)
  • 2021-01-07 00:07

    If you know exactly that the HTML code you want to de-bbcode was en-bbcoded using your method, than do the following:

    Switch the two array you pass to preg_replace.

    In the array with the HTML code, do the following for every element: Prepend # to the string. Append #s. Replace \1 (and \2 aso) with (.*?).

    For the array with the bbcodes do thefollowing with every element: Remove / at the beginning and /ms at end. Replace \s with . Remove all \. Remove all ?. Replace the first (.*) in the string with $1 and the second with $2.

    This should do. If any problems: Ask ;)

    0 讨论(0)
  • 2021-01-07 00:11

    It's pretty safe to say it's nigh impossible to build a reliable way to convert html to bbcode with just a slew of regexes. Use a parser (DOMDocument for instance), remove invalid elements & attributes with xpath's & inspection and then recursively walk it creating a bbcode string on the way (or just ignore invalid tags / attributes on the way).

    0 讨论(0)
提交回复
热议问题