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

  • SEARCH
  • Home
  • 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 9049475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:24:29+00:00 2026-06-16T12:24:29+00:00

I have a big HTML text to parse with a PHP script. I need

  • 0

I have a big HTML text to parse with a PHP script. I need to find (and print on screen) the text between:

<span style="color:COLOR">#</span>

and

<span class="close">#</span>

where COLOR can be “red”, “green” or “blue”.

I wrote THREE loops, each for any color, so my code at the moment is the following:

preg_match_all("'red\">#</span>(.*?)<span class=\"close\">#</span>'si", $text, $match);
foreach($match[1] as $val) {
     echo $val;
}
preg_match_all("'green\">#</span>(.*?)<span class=\"close\">#</span>'si", $text, $match);
foreach($match[1] as $val) {
     echo $val;
}
preg_match_all("'blue\">#</span>(.*?)<span class=\"close\">#</span>'si", $text, $match);
foreach($match[1] as $val) {
     echo $val;
}

Everything works fine but I have two issues with this:

  1. This way I find all the portions of text between red tags, THEN all the portions of text between green tags and FINALLY all the portions of text between blue tags (but I want to find them in the exact order they appear in the text).
  2. All those repeated code… it makes my heart hurt.

So I needed to find a way to search for all the portions of text using an OR condition.

I wrote this piece of code then:

$patterns = array(
    'green\">#<\/span>(.*?)<span class=\"close\">#<\/span>',
    'red\">#<\/span>(.*?)<span class=\"close\">#<\/span>',
    'blue\">#<\/span>(.*?)<span class=\"close\">#<\/span>'
);

$rule= '/(' .implode('|', $patterns) .')/i'; 

$text = 'Lorem ipsum dolor sit amet, <span style="color:red">#</span>consectetur adipiscing elit<span class="close">#</span>. 
Vestibulum ante lectus, <span style="color:green">#</span>pellentesque ac accumsan sit amet, posuere tempor<span class="close">#</span> ligula.';

preg_match_all($rule, $text, $match);
foreach($match[1] as $val) {
    echo "<pre>".$val."</pre><br />";
}

What I expect to find printed on screen:

consectetur adipiscing elit
pellentesque ac accumsan sit amet, posuere tempor

What I actually get:

red">#consectetur adipiscing elit#
green">#pellentesque ac accumsan sit amet, posuere tempor#

So, I’m obviously doing something wrong with the pattern but I can’t find a way to solve this. Any help?

  • 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-16T12:24:31+00:00Added an answer on June 16, 2026 at 12:24 pm

    You can use alternation for this

    (?:red|green|blue)\">#</span>(.*?)<span class=\"close\">#</span>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big problem. I want to extract text from html table that
I have very very big html page/data. I need to fetch data under h1
I have a html/php form like the following: Form 1: Apple options: -big -medium
I have this: <html> <head> <title></title> <meta http-equiv='Content-Type' content='text/html; charset=utf8'> <style type='text/css'> body {
I have a big HTML table (~10K rows, 4 columns, text only) dumped from
I have as input text a big html file from where I have to
I have a big html page, like 4000x6000 px with images and text. I'd
I have a big HTML-string containing multiple child-nodes. Is it possible to construct a
something driver me crazy here i have a big HTML template which i can't
I have quite big document in html format that generated from Microsoft Word. It

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.