html table php,HTML table to php array

--------------EDIT------------------------

So i am going with the DOM approach. Here is what I have so far:

$contents = file_get_contents('internatdata.htm');

//create a DOM based off of the string from the html table

$DOM = new DOMDocument;

$DOM->loadHTML($contents);

//get all tr and td

$items = $DOM->getElementsByTagName('tr');

$tds = $DOM->getElementsByTagName('td');

function tdrows($elements){

$str = "";

for ($ii =0; $ii < $elements->length; $ii++){

$str .= $elements->item($ii)->nodeValue . ",";

}

return $str;

}

for ($i = 0; $i < $items->length; $i++){

echo tdrows($tds) . ";
";

}

}

?>

The issue I am having is that I only want to select the td's from each table row. I am trying to achieve this with a nested loop. unfortunately It is printing the text of every tag on the page how ever many times as there are tags. how can i get it so its only printing the td of each tr and not every td on the dom?

I need to use an html table as the source of my data because I don't have access to the database. I figure to be able to query data from the html table I need create a function to convert the table into an array, or a multidimensional array.

I have the basic Idea I think but I need some help finishing the code to return an array based off the html table.

Also If you have a better way of doing this other than converting the table to an array then please let me know

Here is the idea I had so far:

function getdata(){

$contents = file_get_contents('data.htm');

//add delimiters (semicolon for a row and comma for a cell) ???

$stripped = strip_tags($contents);

//explode into an array based off the delimiters above ???

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值