2013-04-09 31 views
2

我有一組要解析的html項目。我需要解析其類名以'uid-g-uid'結尾的div的內容。下面是樣品的div ...使用PHP DOM解析HTML - 類包含文本

<div class="uid-g-uid">1121</div> 

<div class="yskisghuid-g-uid">14234</div> 

<div class="kif893jduid-g-uid">114235</div> 

我曾嘗試下面的組合,但沒有工作

$doc = new DOMDocument(); 
$bdy = 'HTML Content goes here...'; 
@$doc->loadHTML($bdy); 
$xpath = new DomXpath($doc); 
$div = $xpath->query('//*[@class=ends-with(., "uid-g-uid")]'); 

,也試過

$doc = new DOMDocument(); 
$bdy = 'HTML Content goes here...'; 
@$doc->loadHTML($bdy); 
$xpath = new DomXpath($doc); 
$div = $xpath->query('//*[@class="*uid-g-uid"]'); 

請幫幫忙!

+1

嘗試'// * [結束-用(@類,爲 'uid-G-UID')]' – str 2013-04-09 12:18:07

回答

1

嘗試:

#/ First regex and replace your class with findable flag 
$bdy = preg_replace('/class=\".*?uid-g-uid\"/ims', 'class="__FINDME__"', $bdy); 

#/ Now find the new flag name instead 
$dom = new DOMDocument(); 
@$dom->loadHTML($bdy); 
$xpath = new DOMXPath($dom); 

$divs = $xpath->evaluate("//div[@class = '__FINDME__']"); 
var_dump($divs->length); die(); //check if length is >=1. else we have issue. 

for($j=0; $j<$divs->length; $j++) 
{ 
    $div = $divs->item($j); 
    $div_value = $div->nodeValue; 
    . 
    . 
    . 
} 
+0

,我得到一個錯誤DOMXPath ::評估():xmlXPathCompOpEval:函數結束 - 未找到 – Guns 2013-04-09 12:24:31

+1

ok。你試過'「// div [@class ='* uid-g-uid')]」' – 2013-04-09 12:31:40

+0

這是基本的方案,你將如何得到它。您現在只需要在其中獲得正確的課程搜索。 – 2013-04-09 12:32:12

3

結束-用()需要的Xpath 2.0,所以它不會與DOMXPath這是XPath 1.0中工作。 像這樣的東西應該工作,雖然:

$xpath->query('//*["uid-g-uid" = substring(@class, string-length(@class) - 8)]'); 
+0

這工作,但它不解析HTML ...長度總是返回零... – Guns 2013-04-09 12:37:52

+0

這裏是它沒有正確解析的示例股利...

2201
Guns 2013-04-09 12:40:56

+1

在該股利沒有類屬性 – 2013-04-09 12:51:24

1

你想要做一個XPath 1.0查詢檢查與某些字符串結尾的字符串。字符串函數ends-with()在該版本中不可用。

我可以看到多種方式來做到這一點。至於你的情況的子總是在那裏只有一次,如果再在最後,你可以只使用:

//*[contains(@class, "uid-g-uid")] 

如果子可能是也在有一些其他地方,你不喜歡它,然後檢查如果它是在最後:

//*[contains(@class, "uid-g-uid") and substring-after(@class, "uid-g-uid") = ""] 

如果它可能是在那裏多次,那麼這也不會工作。在這種情況下,你可以檢查字符串結尾室內用它:

//@class[substring(., string-length(.) - 8, 9) = "uid-g-uid"]/.. 

這可能是最直接的變種甚至,或者,作爲substring()第三個參數是可選的比較,直到結束:

//@class[substring(., string-length(.) - 8) = "uid-g-uid"]/.. 
2

由於您正在尋找XPath 1.0中沒有的XPath函數,因此我認爲您可以使用PHP提供的DOMXPath::registerPhpFunctions功能來調用任何PHP函數以進行XPath查詢。這樣,您甚至可以調用preg_match功能是這樣的:

$html = <<< EOF 
<div class="uid-g-uid">1121</div> 
<div class="yskisghuid-g-uid">14234</div> 
<div class="kif893jduid-g-uid">114235</div> 
EOF; 
$doc = new DOMDocument(); 
libxml_use_internal_errors(true); 
$doc->loadHTML($html); // loads your html 
$xpath = new DOMXPath($doc); 

// Register the php: namespace (required) 
$xpath->registerNamespace("php", "http://php.net/xpath"); 

// Register PHP preg_match function 
$xpath->registerPHPFunctions('preg_match'); 

// call PHP preg_match function on your xpath to make sure class ends 
// with the string "uid-g-uid" using regex "/uid-g-uid$/" 
$nlist = $xpath->evaluate('//div[php:functionString("preg_match", 
          "/uid-g-uid$/", @class) = 1]/text()'); 

$numnodes = $nlist->length; // no of divs matched 
for($i=0; $i < $numnodes; $i++) { // run the loop on matched divs 
    $node = $nlist->item($i); 
    echo "val: " . $node->nodeValue . "\n"; 
}