Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 关于查找类的简单问题_Php_Html_Class_Object_Simpledom - Fatal编程技术网

Php 关于查找类的简单问题

Php 关于查找类的简单问题,php,html,class,object,simpledom,Php,Html,Class,Object,Simpledom,我试图做一个简单的提取,但结果总是不可预测 我有这个HTML代码 <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>

我试图做一个简单的提取,但结果总是不可预测

我有这个HTML代码

<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> 
我从错误的配置文件中获取文本,而不是那些与我需要的关联相关的配置文件。 有没有一种方法只需一行代码就能获取所需的信息

就像span profile=“正确的名称”那么 拉它的div-msgbody

好的,我会同意这一点。我不确定“外部文本”应该是什么意思,但我将遵循以下要求:

就像span profile=“正确的名称” 然后拉动它的div msgbody

首先,这里是我使用的缩小的HTML测试用例:

<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>
细分:

//跨度

给我

//span[@class='profile']

告诉我教室在哪里 侧面图

//span[@class='profile'和 包含(,“$profile_name”)]

告诉我教室在哪里 截面轮廓和跨度内部 包含
$profile\u name
,它是 你要的名字

//span[@class='profile'和 包含(,“$profile_name”)]//

告诉我教室在哪里 截面轮廓和跨度内部 包含
$profile\u name
,它是 你要找的名字现在升一级, 这使我们能够

//span[@class='profile'和 包含(,“$profile_name”)]/../div[@class='msgbody']

告诉我教室在哪里 截面轮廓和跨度内部 包含
$profile\u name
,它是 你要找的名字现在升一级, 这让我们来到
,最后,给我
其中类是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}") . "\n";
}
XPath非常强大,就像这样。我建议您查看一个,然后您可以查看,如果您想了解更高级的用法。

好的,我将继续介绍这个。我不确定“外部文本”应该是什么意思,但我将遵循以下要求:

就像span profile=“正确的名称” 然后拉动它的div msgbody

首先,这里是我使用的缩小的HTML测试用例:

<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>
细分:

//跨度

给我

//span[@class='profile']

告诉我教室在哪里 侧面图

//span[@class='profile'和 包含(,“$profile_name”)]

告诉我教室在哪里 截面轮廓和跨度内部 包含
$profile\u name
,它是 你要的名字

//span[@class='profile'和 包含(,“$profile_name”)]//

告诉我教室在哪里 截面轮廓和跨度内部 包含
$profile\u name
,它是 你要找的名字现在升一级, 这使我们能够

//span[@class='profile'和 包含(,“$profile_name”)]/../div[@class='msgbody']

告诉我教室在哪里 截面轮廓和跨度内部 包含
$profile\u name
,它是 你要找的名字现在升一级, 这让我们来到
,最后,给我
其中类是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}") . "\n";
}

XPath非常强大,就像这样。我建议您查看,然后您可以查看,如果您想查看更高级的用法。

这是一个简单的HTML DOM工作示例

我更改了您的示例html,因此Suzy Creamcheese将有多个配置文件,如下所示:(文件:test\u class\u class.htm)


看,它可以用简单的HTMLDOM完成,因为我已经知道DOM是如何工作的。。。或者足以惹上麻烦。。。我不必学习任何语法

这是一个简单的HTML DOM工作示例

我更改了您的示例html,因此Suzy Creamcheese将有多个配置文件,如下所示:(文件:test\u class\u class.htm)


看,它可以用简单的HTMLDOM完成,因为我已经知道DOM是如何工作的。。。或者足以惹上麻烦。。。我不必学习任何语法

哇,这是很多简洁的信息。感谢xpath转换。我喜欢简朴的生活,但它让我记忆深刻!另外,我注意到您必须在标题中插入这个字符以获得那些特殊字符,例如
Jörgenmeier
来传递XPath。
@user734063 hmm有趣。。老实说,您可能只需要
位。这很可能对我有用,因为我在本地保存了文件,并且默认设置了UTF-8。哇,这是很多简洁的信息。感谢xpath转换。我喜欢简朴的生活,但它让我记忆深刻!另外,我注意到您必须在标题中插入这个字符以获得那些特殊字符,例如
Jörgenmeier
来传递XPath。
@user734063 hmm有趣。。老实说,您可能只需要
位。它很可能对我有效,因为我在本地保存了文件,并且默认设置了UTF-8。
 <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>
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.
---------------------------------