There is a line like:

some text [tagname = "param1"] some text [/ tagname] some other text

You need to parse the text using PHP tools, pull each similar construction out of it, and write each into a dictionary like:

[ ['name' => 'Tagname', 'param' => 'param1', 'data' => 'some text'], ... ] 

I tried to make it so that at first parse at least opening tags:

 $string1 = "some text[AAA=\"bbb\"]some stuff[/AAA]some more text[sdfsf=\"bbb\"]some stuff[/sdfsf] sdfsdfsdfs"; $reg1 = "#\[[Az]\=\"[Az]\"\]#"; $matches = []; preg_match_all($reg1, $string1, $matches); print_r($matches); 

but matches with the template are not output to the array. Only one empty element is visible. Maybe preg_match_all is doing something not exactly what I imagine, and need some other functions?

  • one
    [Az]+ , otherwise only 1 letter should be. Yes, and the interval Az is not only Latin letters. - Visman
  • Once done a BB-code parser. Regulars are a bad move. Parse by occurrences [ , something like strpos('[') . - user207618
  • @Other, better regulars for parsing bb-codes or nothing. Otherwise, you will learn to write the rules. - Visman
  • No, a good AST is quite simply written. - user207618
  • @Other, and even the fact that the parameters can have single and double quotes, or they may not be; inside parameters there may be the same quotes in which they are enclosed, as well as closing square brackets; And the parameters can be pieces of 10-11 in a mix? - Visman

1 answer 1

In general, if there is an opportunity, use html tags. If not, then you need to write a parser algorithm:

  1. take the opening tag. we put in the stack
  2. We are looking for a closing tag. we put in the stack
  3. if there are parameters parsim them ie It has a parameter and after the name is equal to and two quotes "or" but not as "" are not different. We lay down in stack
  4. On this principle, we check the validity of tags, i.e. the number of elements in the stack should be divided into 2-va without residue.
  5. Next, from the stack we take the values ​​we need.

https://github.com/jbowens/jBBCode is a good example of implementation.

ps regular schedule does not work for several reasons:

  1. Cannot validate data.
  2. vulnerability of such decisions
  3. speed, regulars it is slow and resource-intensive. The regular schedule is recursively symbolically, so the complexity is from O ^ 2 to O ^ n depending on the pattern.
  • About regulars complete lies! - Visman