1

I have a string like this in PHP

<li>bla bla bla bla</li>
<li>hello hello</li>
<li>the brown fox didnt jump</li>
.
.
.
<li>aaaaaaarghhhh</li>

I want to get a string wich contains the first X li's (li tags included in the result string)

<li>.....first one....</li>
<li>.....second one....</li>
<li>.................</li>
<li>.....X one....</li>

How can this be done with REGEX or something else????

I could remove the

</li>

then explode by

<li>

and get the first X elements of array and then adding again li tags at beginning and at end of each element, but i think its too dirty...

Any better ideas?

easy_weezy
  • 101
  • 2
  • 6
  • 1
    Famous answer: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Kirill Polishchuk Jul 18 '11 at 10:12
  • 2
    i don't ask for a complete HTML parser, this is a string without any other HTML tags except for
  • and
  • . I want to match X times a string that starts with
  • and ending with
  • . If you dont like li's replace
  • with "Dogs" and
  • with "Cats". – easy_weezy Jul 18 '11 at 10:18