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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:04:59+00:00 2026-05-22T15:04:59+00:00

I am trying to do a simple extraction, but I keep ending up with

  • 0

I am trying to do a simple extraction, but I keep ending up with unpredictable results.

I have this HTML code

<div class="thread" style="margin-bottom:25px;"> 

<div class="message"> 

<span class="profile">Suzy Creamcheese</span> 

<span class="time">December 22, 2010 at 11:10 pm</span> 

<div class="msgbody"> 

<div class="subject">New digs</div> 

Hello thank you for trying our soap. <BR>  Jim.

</div> 
</div> 


<div class="message reply"> 

<span class="profile">Lars Jörgenmeier</span> 

<span class="time">December 22, 2010 at 11:45 pm</span> 

<div class="msgbody"> 

I never sold you any soap.

</div> 

</div> 

</div> 

And I am trying to extract the outertext from “msgbody” but only when the “profile” is equal to something. Like so.

$contents  = $html->find('.msgbody');
$elements = $html->find('.profile'); 

           $length = sizeof($contents);

           while($x != sizeof($elements)) {

            $var = $elements[$x]->outertext;

                        //If profile = the right name
            if ($var = $name) {

                                    $text = $contents[$x]->outertext;
                echo $text;

            }



            $x++;
         }    

I get text from the wrong profiles, not the ones with the associations I need.
Is there a way to just pull the desired info with one line of code?

Like if span-profile = “correct name” then
pull its div-msgbody

  • 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-22T15:05:00+00:00Added an answer on May 22, 2026 at 3:05 pm

    Okay I’m going to go with DOMXpath on this one. I’m not sure what ‘outer text’ is supposed to mean, but I’ll go with this requirement:

    Like if span-profile = "correct name"
    then pull its div-msgbody

    First off, Here’s the minified HTML test case I used:

    <html>
    <body>
    <div class="thread" style="margin-bottom:25px;"> 
    
    <div class="message"> 
    
    <span class="profile">Suzy Creamcheese</span> 
    
    <span class="time">December 22, 2010 at 11:10 pm</span> 
    
    <div class="msgbody"> 
    
    <div class="subject">New digs</div> 
    
    Hello thank you for trying our soap. <BR>  Jim.
    
    </div> 
    </div> 
    
    
    <div class="message reply"> 
    
    <span class="profile">Lars Jörgenmeier</span> 
    
    <span class="time">December 22, 2010 at 11:45 pm</span> 
    
    <div class="msgbody"> 
    
    I never sold you any soap.
    
    </div> 
    
    </div> 
    
    </div>
    </body>
    </html>
    

    So, we’ll make an XPath query for this. Let’s show the whole thing, then break it down:

    $messages = $xpath->query("//span[@class='profile' and contains(.,'$profile_name')]/../div[@class='msgbody']");
    

    The break down:

    //span

    Give me spans

    //span[@class=’profile’]

    Give me spans where the class is
    profile

    //span[@class=’profile’ and
    contains(.,’$profile_name’)]

    Give me spans where the class is
    profile and the inside of the span
    contains $profile_name, which is the
    name you’re after

    //span[@class=’profile’ and
    contains(.,’$profile_name’)]/../

    Give me spans where the class is
    profile and the inside of the span
    contains $profile_name, which is the
    name you’re after now go up a level,
    which gets us to <div class="message">

    //span[@class=’profile’ and
    contains(.,’$profile_name’)]/../div[@class=’msgbody’]

    Give me spans where the class is
    profile and the inside of the span
    contains $profile_name, which is the
    name you’re after now go up a level,
    which gets us to <div class="message"> and finally, give me
    all divs under <div class="message">
    where the class is msgbody

    Now then, here’s a sample of the PHP code:

    $doc = new DOMDocument();
    $doc->loadHTMLFile("test.html");
    
    $xpath = new DOMXpath($doc);
    $profile_name = 'Lars Jörgenmeier';
    $messages = $xpath->query("//span[@class='profile' and contains(.,'$profile_name')]/../div[@class='msgbody']");
    foreach ($messages as $message) {
      echo trim("{$message->nodeValue}") . "\n";
    }
    

    XPath is very powerful like this. I recommend looking over a basic tutorial, then you can check the XPath standard if you want to see more advanced usage.

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

Sidebar

Related Questions

I am trying some simple code on fork. When I give code like this,
I am trying this simple tutorial from oracle : http://www.oracle.com/technetwork/java/socket-140484.html (the Example 1 ).
I am trying something very simple, but for some reason it does not work.
I am trying to write simple Visual Studio Add-In for code generation. In my
I am trying a simple JAXB marshaling in my JUit test class and I
Well this is probably a stupid question with a simple answer but when using
I'm trying this simple query: SELECT * FROM `Users` WHERE MATCH (`User`) AGAINST ('User')
I am trying this simple ctypes example and getting the error mentioned >>> from
I'm trying simple example - #include <stdio.h> int main(void) { printf(Content-type: text/html\n\n); printf(<html><title>Hello</title><body>\n); printf(Goodbye
I am trying a simple Extension Method example and am unable to increment or

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.