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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:52:47+00:00 2026-05-23T10:52:47+00:00

Here are some codes, how to get the div[class=title] and the nearest <p> content?

  • 0

Here are some codes, how to get the div[class=title] and the nearest <p> content? I only know how to get the div[class=title] in a foreach, but no idea how to get the p. Thanks.

<?php
header("Content-type: text/html; charset=utf-8");
require_once("simple_html_dom.php");
?>
<?php
$str = <<<ETO
<div id="content">
<div class="title"><p>text1</p></div>
<p>destriction1</p>
<p>destriction2</p>
<div class="title"><p>text2</p></div>
<p>destriction3</p>
<p>destriction4</p>
<p>destriction5</p>
<div class="title"><p>text3</p></div>
<p>destriction6</p>
<p>destriction7</p>
</div>
ETO;
$html = str_get_html($str);
foreach($html->find("div[class=title]") as $content){
    echo $content.'<hr />';
}
?>

I want output like:

text1
destriction1
destriction2
------------------------------
text2
destriction3
destriction4
destriction5
------------------------------
text3
destriction6
destriction7
------------------------------
  • 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-23T10:52:47+00:00Added an answer on May 23, 2026 at 10:52 am

    Have you tried a selector like div[class=title] p?

    $html = str_get_html($str);
    foreach($html->find("div[class=title] p") as $content){
        echo $content.'<hr />';
    }
    

    The children() function should also work (shown below)

    That will get you the <p> in each title div, although not the following paragraphs. To do that, use the next_sibling() function. Something like this:

    $html = str_get_html($str);
    foreach($html->find("div[class=title]") as $content){
        // $content = <div class="title">.  first_child() should be the <p>
        echo $content->first_child().'<hr />';
    
        // Get the <p>'s following the <div class="title">
        $next = $content->next_sibling();
        while ($next->tag == 'p') {
            echo $next.'<hr />';
            $next = $next->next_sibling();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML Data which looks like: <div id=foo><a class=someClass href=http://somelink>Some Title</a></div> <div
Here is some code I could not get to format properly in markdown, this
I am trying to get some form data from POST method. Here's the code
Here is some code: class Person def initialize(age) @age = age end def age
Not the aggregate delta but the delta of each element. Here is some code
Here's some of the code, the box displays fine but none of the options
I want to make a PHP JSON data foreach, but I met some problem.
Here's some code I saw once. Can you see what's wrong with it? [updated]
Here's some code (full program follows later in the question): template <typename T> T
Here's some code: DirectorySearcher searcher = new DirectorySearcher(); searcher.Filter = (&(objectClass=user)(sAMAccountName= + lstUsers.SelectedItem.Text +

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.