I have a field of text where users can write specific command to get some html.
For example:
Text Text Text Text
[*] first entry
[*] second entry
[*] 3rd...
Text Text
This text should be converted with regex to something like this:
text text
<ol>
<li>FirstEntry</li>
<li>Second</li>
..
</ol>
text text
Any suggestion?
The regex that matches the line with entry is something like this:
/\[\*\].+/i
The problem is how to insert correctly the <ol> and </ol>
My solution
I was thinking I could parse all line of text and when the parser encouter the first line that starts with [*] then put an <ol> Same thing for the </ol>
Until now I have made the script that converst single [*] ... to <li> ...
preg_replace('/\[\*\](.+)/i','<li>$1</li>',$str);
I need the <ol> part
Doing this kind of parsing with regular expressions is probably not the way to go; it might be OK for very simple scenarios but the more features you try to put in the harder it will get — until the point where it becomes unmaintainable.
I would suggest borrowing parsing techniques from e.g. various MarkDown parsers out there; see these questions for more information (there are others too).
Back to the original question: if you are constrained to only using regular expressions you could try matching the whole “list block” first with something like
(the multiline mode modifier is essential)
You can then produce the output by breaking the matched text down into individual list items with your current regex and then surrounding the result with
<ol></ol>.