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:
- 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.
- You can get the inner text with the
textContent
[docs] property.
ID必须是唯一的,因此循环元素并搜索具有特定ID的元素几乎没有意义。直接获取元素。
您可以使用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:
- 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.
- You can get the inner text with the
textContent
[docs] property.
ID必须是唯一的,因此循环元素并搜索具有特定ID的元素几乎没有意义。直接获取元素。
您可以使用textContent [docs]属性获取内部文本。
Example:
$div = $doc->getElementById('result');
if($div) {
echo $div->textContent;
}