如何从值 div>获取价值?[英]How to get value from value? 本文翻译自  Aryan G  查看原文  2011-12-05  17465    domdocument/

时间:2022-01-21 20:29:39

I need to get value from

我需要从中获得价值

<div id = "result">Roll No 103 Pass</div> 

and out put should be : Roll No 103 Pass

并且输出应该是:Roll No 103 Pass

I used this code :

我用过这段代码:

$markup = file_get_contents('www.results.com');
$doc = new DomDocument();
@$file = $doc->loadHTML($markup);
$spans = $doc->getElementsByTagName('div');
foreach($spans AS $span)
    {
    $class = $span -> getElementsById('id');
    if($class=="result") { 
        echo $span -> nodeValue;


    }


}

but it just return blank screen

但它只是返回空白屏幕

2 个解决方案

#1


3  

$doc = new DomDocument();
$doc->loadHTMLFile('http://www.results.com');
$thediv = $doc->getElementById('result');
echo $thediv->textContent;

#2


1  

Two remarks:

  1. IDs have to be unique, so there is little sense in looping over elements and search for an element with a specific ID in them. Just get the element directly.
  2. ID必须是唯一的,因此循环元素并搜索具有特定ID的元素几乎没有意义。直接获取元素。

  3. You can get the inner text with the textContent [docs] property.
  4. 您可以使用textContent [docs]属性获取内部文本。

Example:

$div = $doc->getElementById('result');
if($div) {
    echo $div->textContent;
}

#1


3  

$doc = new DomDocument();
$doc->loadHTMLFile('http://www.results.com');
$thediv = $doc->getElementById('result');
echo $thediv->textContent;

#2


1  

Two remarks:

  1. IDs have to be unique, so there is little sense in looping over elements and search for an element with a specific ID in them. Just get the element directly.
  2. ID必须是唯一的,因此循环元素并搜索具有特定ID的元素几乎没有意义。直接获取元素。

  3. You can get the inner text with the textContent [docs] property.
  4. 您可以使用textContent [docs]属性获取内部文本。

Example:

$div = $doc->getElementById('result');
if($div) {
    echo $div->textContent;
}