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 8529809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:06:49+00:00 2026-06-11T09:06:49+00:00

Possible Duplicate: How to parse and process HTML with PHP? Hi there I have

  • 0

Possible Duplicate:
How to parse and process HTML with PHP?

Hi there I have scraped a web page

  <div class="col blue">
        <img  src="/en/media/Dentalscreenresized.jpg" />
        <h4>This is line i want to scrape</h4>
        <p class="date">12 Sep
            <span class="year">2012</span></p>
        <p>13 people were diagnosed with oral cancer after last year&rsquo;s Mouth Cancer Awareness Day. Ring 021-4901169 to arrange for a free screening on the 19th September.</p>
        <p class="readmore"><a href="/en/news/abcd.html">Read More</a></p>
        <p class="rightreadmore"><a href="http://www.xyz.ie/en/news/">See all News&nbsp;&nbsp;&nbsp;</a></p>
    </div>

Now i want to display the <h4> tag of class="col blue".I seen online to use preg_match_all() I am not familiar with regular expression… any help please

  • 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-11T09:06:50+00:00Added an answer on June 11, 2026 at 9:06 am

    Well, as often in life, you have two options here (I assume that scraped page’s content is stored in $content variable):

    The Way of (Cthulhu) Regex:

    $matches = array();
    preg_match_all('#<div class="col blue">.+?<h4>([^<]+)#is', $content, $matches);
    var_dump($matches[1]);
    

    The Way of DOM Parsing:

    $dom = new DOMDocument();
    $dom->loadHTML($content);
    $xpath = new DOMXpath($dom);
    $elements = $xpath->query('//div[@class="col blue"]/h4');
    foreach ($elements as $el) {
       var_dump($el->textContent);
    }
    

    And the real question, of course, is what way to choose.

    The first option is short, concise, and quite seductive overall. I admit I would use it once, twice, or (pony he comes) even more – IF and only IF I know that HTML I work with will always come to me normalized AND I can cope with its structure suddenly changing in a non-predictable way.

    The second option is slightly bigger, and may look too generic. Yet it’s way more flexible and resilient to changes in the source, in my opinion.

    For example, consider what happens if some ‘blue-colored’ divs in the source HTML may come out without <h4> element. To work correctly in such conditions, the regex will have to become much more sophisticated. And the XPath query? Won’t change – even a slightest bit.

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

Sidebar

Related Questions

Possible Duplicate: How to parse and process HTML with PHP? I have HTML document
Possible Duplicate: How to parse and process HTML with PHP? I have HTML code
Possible Duplicate: How to parse and process HTML with PHP? i get page with
Possible Duplicate: How to parse and process HTML with PHP? I am brand new
Possible Duplicate: How to parse and process HTML with PHP? Here is what I'm
Possible Duplicate: How to parse and process HTML with PHP? $content = <p>This is
Possible Duplicate: How to parse and process HTML/XML with PHP? I want to grab
Possible Duplicate: How to parse and process HTML with PHP? I'm not very good
Possible Duplicate: How to parse and process HTML with PHP? I'm looking into HTML
Possible Duplicate: How to parse and process HTML with PHP? I'm trying to use

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.