I need a PHP regular expression pattern to select separately all lists <ul></ul> from string.
The string is like:
Lorem ipsum dolor sit amet,...
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
Lorem ipsum dolor sit amet,...
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
....
I need to extract both lists and save them in array, so the result would look like:
$listsarray[0] = first list code from <ul> to </ul>.
$listsarray[1] = second list code, etc..
What I have tried, but this doesn’t work as expected. If there is more than two lists, it selects first two as one (I don’t know why, I’m a novice at regular expressions):
$content = 'the content like above...';
$pattern = '/<ul[^.]*<\/ul>/';
preg_match_all($pattern, $content, $listsarray)
Don’t use regular expressions to parse HTML, it’s a bad idea as HTML is not a regular language… You can use other methods such as tidy or the built in DOMDocument to parse it easily without regular expressions
If you insist, what you’re looking for is reluctant matching (instead of greedy)
change
*to*?See this post about the difference and this one on why it’s a bad idea to try and parse html with regular expressions