试图在PHP中获取标签的值

时间:2013-10-29 11:49:55

标签: php xpath domdocument

<span class="p_txt1 p_1">
<input type="checkbox" value="value 1">
<b>value 2</b>
</span>
<span class="p_txt1 p_1">
<input type="checkbox" value="value 1">
<b>value 2</b>
</span>
<span class="p_txt1 p_1">
<input type="checkbox" value="value 1">
<b>value 2</b>
</span>

我正在尝试从输入获取值1,从粗体标记获取值2,但它不起作用

$doc = new DOMDocument();
@$doc->loadhtml($contents);
$xpath = new DOMXPath($doc);
$dataString =  "";
$fruits = $xpath->query("//span[@class='p_txt1 p_1']");
foreach($fruits as $fruit) { 
echo $xmlDate = $fruit->getElementsByTagName( "b" ),"<br>";
}

任何人都可以提供帮助

1 个答案:

答案 0 :(得分:1)

试试这个:

$doc = new DOMDocument();
@$doc->loadhtml($contents);
$xpath = new DOMXPath($doc);
$dataString =  "";
$fruits = $xpath->query("//span[@class='p_txt1 p_1']");
foreach($fruits as $fruit) {
   foreach ($fruit->getElementsByTagName( "b" ) as $xmlBold) {
        echo $xmlBold->nodeValue.'<br/>';
   }
}