如何在简单的html dom中计算属性元素的数量

时间:2017-06-11 20:40:20

标签: php html domdocument simple-html-dom

我有这个代码,它假设从HOME |获取数据并安排在表格中绘图| AWAY,当您查看代码时,您会发现count变量被赋予一个静态变量,当获取的值的数量将大于指定的变量时,它将带来问题。所以我需要的帮助是如何确定的或者我将用什么函数来计算这个属性的元素数量

'div[data-gamename="1X2"' 

如下面的代码所示,如​​何计算它们?

<?php
include('advanced_html_dom.php'); 
$html = file_get_html('your-url-here');

 //Here is where should be a counter to be assigned to $count variable
$count = 450;
for ($i=0; $i<$count; $i+=3){
$dot = $html->find('div[data-gamename="1X2"]',$i);
echo " Home ".$dot."<br>";

 }

//This codes will be re-written to look same like the above
//I left for reference so you can get picture what i mean
$dot2 = $html->find('div[data-gamename="1X2"]',1);
$dot3 = $html->find('div[data-gamename="1X2"]',2);

 echo "Draw ".$dot2."<br>";
  echo "Away ".$dot3."<br>";

 $html->clear();
 ?>

知道如何实现这个吗?

1 个答案:

答案 0 :(得分:1)

 $numcounter = count($dot2 = $html->find('div[data-gamename="1X2"]'));
 echo "Number of elements are ".$numcounter." ------";

这是未来参考的答案。应将$numcounter变量分配给$count变量,并从上面的代码中将其变为$count = $numcounter;