How can I match the first unordered list from a string and select the first list items? <p>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:</p> <pre><code>/&lt;\s*ul[^&gt;]*&gt;(.*?)&lt;\s*/\s*ul&gt;/s </code></pre> <p>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:</p> <pre><code>&lt;ul class="mylist"&gt; &lt;li&gt;Lorem Ipsum Dolor Sit Amet&lt;/li&gt; &lt;li&gt;Lorem Ipsum Dolor Sit Amet&lt;/li&gt; &lt;li&gt;Lorem Ipsum Dolor Sit Amet&lt;/li&gt; &lt;li&gt;Lorem Ipsum Dolor Sit Amet&lt;/li&gt; &lt;li&gt;Lorem Ipsum Dolor Sit Amet&lt;/li&gt; &lt;li&gt;Lorem Ipsum Dolor Sit Amet&lt;/li&gt; &lt;li&gt;Lorem Ipsum Dolor Sit Amet&lt;/li&gt; &lt;li&gt;Lorem Ipsum Dolor Sit Amet&lt;/li&gt; &lt;li&gt;Lorem Ipsum Dolor Sit Amet&lt;/li&gt; &lt;li&gt;Lorem Ipsum Dolor Sit Amet&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>and I want to create the same one with only the first 5 <code>&lt;li&gt;</code> tags, so should I use regex or some other methods in php ?</p> <p>and thanks in advance.</p>