Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8191407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:54:49+00:00 2026-06-07T03:54:49+00:00

I need a PHP regular expression pattern to select separately all lists <ul></ul> from

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:54:51+00:00Added an answer on June 7, 2026 at 3:54 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to match using regular expression in php addresses like: 144 street, city,
I have a string like this ch:keyword ch:test ch:some_text I need a regular expression
I need help with writing regular expression for this pattern in PHP: [[{type:media,view_mode:small,fid:1,attributes:{width:0,height:0,src:http://localhost/x.png}}]] This
I need a regular expression in Javascript, as well as in PHP, for Indian
The regular expression ^/wp-[^./]+\.php$ matches: wp-config.php wp-commentsrss2.php wp-pass.php wp-rdf.php [...] But I need another
I need a PHP 5 regular expression for the following date/time format: 10 Jul
I need help on regular expression here. I want PHP to be able to
I asked a question about regular expression in PHP (which got answered), I need
I give up. I need a (PHP) regular expression that matches only 5 digit
I need to write a regular expression using php which parses the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.