2

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> ...

http://codepad.org/yzRVupON

preg_replace('/\[\*\](.+)/i','<li>$1</li>',$str);

I need the <ol> part

My implementation

http://codepad.org/NNgC6uko

  • You want to take a look at phps regex function `preg_replace_callback()`. – arkascha Nov 10 '12 at 14:56
  • Thanks for the input.. but I knew that function and I Don't see how I could use that –  Nov 10 '12 at 14:58
  • Why, you use a regex to match the patterns `[*] first entry` and so on and for each such match the callback function you implement is called. Inside that function you can output whatever markup you want, for example `
  • FirstEntry
  • `. – arkascha Nov 10 '12 at 14:59
  • That's not the question :). The question is more like how to insert correctly
      and
    –  Nov 10 '12 at 15:00
  • Well, maybe by doing the same using another regex pattern? Though that is even easier, you don't need a callback but can use the plain `preg_replace()` function. – arkascha Nov 10 '12 at 15:02
  • Please make an example: http://codepad.org/8OyDau57 –  Nov 10 '12 at 15:02
  • Sorry, no, I won't make your homework :-) You claim you know how to use the functions, so use them. Post what you have so far, your attempts and say what specific part it is you cannot get to work the way you want to. Please understand that this is not a forum to get free labor. – arkascha Nov 10 '12 at 15:04
  • @arkascha: that's not an homework. It is a suggestiong how to proceed. I have made the simple script that converts that with
  • http://codepad.org/yzRVupON but i still need suggestiong for
      part
  • –  Nov 10 '12 at 15:08
  • Sorry, but that 'simple script' you mention does not at all consider the hints I gave you. – arkascha Nov 10 '12 at 15:14
  • @arkascha: Thanks for help anyway. If you know and if you want post an answer –  Nov 10 '12 at 15:16