2016-11-05 159 views
2

我想用PHP检索表格单元格的内容到另一个文件。使用此代码,我检索全部单元的内容。PHP获取单元格内容

$url = 'folder/myPage.php'; 
$content = file_get_contents($url); 
$first_step = explode('<tr id="foo">' , $content); 
$second_step = explode('</tr>' , $first_step[1]); 

echo $second_step[0]; 

如何检索特定的单元格? (第二个,例如...)

$url = 'folder/myPage.php'; 
$content = file_get_contents($url); 

//Ugly Code !...doesn't work ! 

$first_step = explode('<tr id="foo"><td[1]' , $content); 
$second_step = explode('</td></tr>' , $first_step[1]); 

echo $second_step[0]; 

Thanks.Nicolas。

+1

您不应该使用PHP来检查DOM文档,但无论如何..如果您确实使用了[library](http://simplehtmldom.sourceforge.net/)。 – Xorifelse

+0

我会尽力的。谢谢。 –

+0

如果它不是PHP,它是AJAX? Jquery? –

回答

1

我已经找到了解决方案!第二个爆炸与</td>而不是与</tr>

$url = 'folder/myPage.php'; 
$content = file_get_contents($url); 
$first_step = explode('<tr id="' . $ref . '">' , $content); 
$second_step = explode('</td>' , $first_step[1]); 

echo $second_step[1]; // show the content of the second cell 
echo $second_step[5]; // show the content of the sixth cell 
// etc... 

// "$ref" is a loop with foreach in a list of elements 

我不想使用图书馆做这件事(比如Simple_Html_Dom:1800行代码,只为了看文件!太沉重)

有了这个解决方案,它的工作就像一个魅力 !我很高兴.. :-) [解决]。 Nicolas

0

你需要通过<td>职高<td>也爆炸是一个“表格单元格” - <tr>是整排

+0

两个“explode”?如何写这个? –

+0

如果你真的想这样做像这样,比第一次爆炸tr之后,你会有行数组,在每个元素(行)上调用td爆炸会给你一个单元格数组(你需要)。但是,它不是一个推荐的方法处理一般的DOM。 – KamKo

+0

我找到了解决办法!看看我的帖子下面。Nicolas。 –