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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:07:48+00:00 2026-05-12T19:07:48+00:00

I have the contents of a web page assigned to a variable $html Here’s

  • 0

I have the contents of a web page assigned to a variable $html

Here’s an example of the contents of $html:

<div class="content">something here</div>
<span>something random thrown in <strong>here</strong></span>
<div class="content">more stuff</div>

How, using PHP can I create an array from that that finds the contents of <div class="content"></div> regions like this (for the example above) so:

echo $array[0] . "\n" . $array[1]; //etc

outputs

something here
more stuff
  • 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-05-12T19:07:48+00:00Added an answer on May 12, 2026 at 7:07 pm

    Assuming this is just a simplified case in the OP and the real situation is more complicated, you’ll want to use XPath.

    If it’s really complex, then you may want to use DOMDocument (with DOMXPath), but here’s a simple example using SimpleXML

    $xml = new SimpleXMLElement($html);
    
    $result = $xml->xpath('//div[@class="content"]');
    
    while(list( , $node) = each($result)) {
        echo $node,"\n";
    }
    

    Since you explicitly asked about creating an array for this, you could use:

    $res_Arr = array();
    while(list( , $node) = each($result)) {
        $res_Arr[] = $node;
    }
    

    and $res_Arr would be an array with the contents you’re looking for.

    See http://php.net/manual/en/simplexmlelement.xpath.php for php SimpleXML Xpath info and http://www.w3.org/TR/xpath for the XPath specifications

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

Sidebar

Related Questions

So I have simple html web page with one div with id=toFill on it.
I have a wrapper class that contains all the content on the web page.
On my web site I have XMLs with my page contents (automatically generated from
I have a web page in which a fair amount of the content is
I have an image on my web page like this: <img src=/Images/content/home.png alt= style=float:
I have a web application that has a page that loads the content from
I have implemented UIWebView and displaying the contents coming from web services,Here is the
I have a web page that has a DIV and an IFRAME. When a
I have a web page that contains a div element. On the page, there
I have a web page, and after the contact form is submitted, the contents

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.