Reputation: 13046
I have a content string that starts with an unordered list I want to make a summary of this content on my homepage and I need to match the first unordered list and only show 5 list items in the preview, so I stated with matching the whole ul tag using this regex:
/<\s*ul[^>]*>(.*?)<\s*/\s*ul>/s
it works fine in regex tester online but I get Unknown modifier '\' and I don't know which one ? also after getting the whole unordered list how can I choose only the first 5 list items for example:
<ul class="mylist">
<li>Lorem Ipsum Dolor Sit Amet</li>
<li>Lorem Ipsum Dolor Sit Amet</li>
<li>Lorem Ipsum Dolor Sit Amet</li>
<li>Lorem Ipsum Dolor Sit Amet</li>
<li>Lorem Ipsum Dolor Sit Amet</li>
<li>Lorem Ipsum Dolor Sit Amet</li>
<li>Lorem Ipsum Dolor Sit Amet</li>
<li>Lorem Ipsum Dolor Sit Amet</li>
<li>Lorem Ipsum Dolor Sit Amet</li>
<li>Lorem Ipsum Dolor Sit Amet</li>
</ul>
and I want to create the same one with only the first 5 <li>
tags, so should I use regex or some other methods in php ?
and thanks in advance.
Upvotes: 0
Views: 410
Reputation: 13557
/<\s*ul[^>]*>(.*?)<\s*/\s*ul>/s
you need to escape the /
if you use it as the delimiter.
/<\s*ul[^>]*>(.*?)<\s*\/\s*ul>/s
in PHP you can use any character as the delimiter, though:
#<\s*ul[^>]*>(.*?)<\s*/\s*ul>#s
You can repeat a pattern:
#<\s*ul[^>]*>(\s*<li>.+?</li>){5}#sm
will match 5 <li>
s. You won't be able to access them seperately, though. You can either unroll that repeated-group, or run a second expression to extract the <li>
s.
Upvotes: 2