English 中文(简体)
简而言之,DOM D. 寻找班级的问题
原标题:Simple_DOM question about finding classes

我正试图进行简单开采,但我仍然以无法预测的结果结束。

我有这一超文本。

<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> 

而且,我试图从“人”中提取外文,但只有在“人”与东西相等时。 类似情况。

$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

最佳回答

Okay I m 前往DOMXpath。 我不敢确定什么东西应该意味着什么,但我不赞同这一要求:

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

首先, 此处使用的是:

<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>

因此,我们对此提出警告。 让我表明整个情况,然后打破:

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

破裂:

//span

宽度

页: 1

宽度 where the class is profile

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

宽度 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 )]/../

宽度 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 ]

宽度 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

如今,这里有一套PHP代码样本:

$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}") . "
";
}

营养餐像这样强大。 我建议查看basic tutorial ,然后请查看。 XPath standard 如果你希望看到更先进的使用。

问题回答

这是一种简单的超文本广播公司工作实例。

I changed your example html so there would be more than one profile for Suzy Creamcheese as follows: (file: test_class_class.htm)

 <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>

 <div class="message"> 
   <span class="profile">Suzy Yogurt</span> 
   <span class="time">December 22, 2010 at 11:10 pm</span> 
   <div class="msgbody"> 
     <div class="subject">No Creamcheese</div> 
       This is not Suzy Creamcheese <BR>  Jim.
     </div> 
   </div> 

   <div class="message reply"> 
     <span class="profile">Suzy Creamcheese</span> 
     <span class="time">December 22, 2010 at 11:45 pm</span> 
     <div class="msgbody"> 
       A reply from Suzy Creamcheese.
     </div> 
   </div> 
 </div>

</div>

Here is my test using Simple HTML DOM: include( simple_html_dom.php );

function getMessage_for_profile($iUrl,$iProfile)
{
    // create HTML DOM
    $html = file_get_html($iUrl);

    // get text elements
    $aoProfile = $html->find( span[class=profile] ); 
    echo "Found ".count($aoProfile)." profiles.<br />";

    foreach ($aoProfile as $key=>$oProfile)
    {
      if ($oProfile->plaintext == $iProfile)
      {
        echo "<b>Profile ".$key.": ".$oProfile->plaintext."</b><br />";
// Using $e->next_sibling ()
        $oCurrent = $oProfile;
        while ($oNext = $oCurrent->next_sibling())
        {
           if ( $oNext->class == "msgbody" )
           {
             echo "<hr />";
             echo $oNext->outertext;
             echo "<hr />";
           }
           $oCurrent = $oNext;
        }
      }         
    }

    // clean up memory
    $html->clear();
    unset($html);

    return;
}
// --------------------------------------------
// test it!
// user_agent header...
ini_set( user_agent ,  My-Application/2.5 );

getMessage_for_profile( test_class_class.htm , Suzy Creamcheese );
echo "<br /><br /><br />";
getMessage_for_profile( test_class_class.htm , Suzy Yogurt );

我的产出是:

Found 4 profiles.
Profile 0: Suzy Creamcheese
--------------------------------
New digs
Hello thank you for trying our soap.
Jim.
---------------------------------
Profile 3: Suzy Creamcheese
---------------------------------
A reply from Suzy Creamcheese.
---------------------------------



Found 4 profiles.
Profile 2: Suzy Yogurt
---------------------------------
No Creamcheese
This is not Suzy Creamcheese
Jim.
---------------------------------

可以通过简单的超文本处理,因为我已经知道OMM如何工作......或足以制造麻烦......。 我无需学习任何知道的同yn!





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签