2013-04-26 41 views
1
<tr class='Jed01'> 
<td height='20' class='JEDResult'>1</td> 
<td height='30' class='JEDResult'>26.04.2013</td> 
<td height='30' class='JEDResult'>19:43</td> 
<td height='30' class='JEDResult'>Processing</td> 
<td height='30' class='JEDResult'><a href="#" pressed="GetInfo(1233);" title=''>Jeddah</a></td> 
</tr> 

结果=第一步 - 日期 - 时间 - 状态 - 地方DOM解析问题空的结果

首先,我是新来的PHP,我试图通过PHP这个数据解析到我的网页 - DOM正如在Stackoverflow之前向我推荐的那样。在下面的代码中,我调用了所有的类来获取数据,但在没有任何问题的时候我无法得到任何结果。那么请问哪里可能是我的问题?

由于从现在

<?php 

$input = "www.kalkatawi.com/luai.html" 
$html = new DOMDocument(); 
$html->loadHTML($input); 


foreach($html->getElementsByTagName('tr') as $tr) 
{ 
    if($tr->getAttribute('class') == 'Jed01') 
    { 
    foreach($html->getElementsByTagName('td') as $td) 
    { 
     if($td->getAttribute('class') == 'JEDResult') 
     { 
     echo ($td->nodeValue); 
     } 
    }  
    } 
} 

?> 
+2

你必须在第一行代码的一些语法错误更容易做到这一点:'$输入= 'myLink的';'' – Sirko 2013-04-26 09:14:44

+0

预计loadHTML''$输入*本身*为HTML,在你的例子中它不是。这是怎么回事? – Jon 2013-04-26 09:17:54

+0

不知道如何从作者看到这种语法错误?写在记事本上? – 2013-04-26 09:18:09

回答

2

不要忘记那些半冒号;)

试试这个;

<?php 

$input = file_get_contents("http://www.kalkatawi.com/luai.html"); 
$html = new DOMDocument(); 
$html->loadHTML($input); 


foreach($html->getElementsByTagName('tr') as $tr) 
{ 
    if($tr->getAttribute('class') == 'Jed01') 
    { 
    foreach($tr->getElementsByTagName('td') as $td) 
    { 
     if($td->getAttribute('class') == 'JEDResult') 
     { 
     echo ($td->nodeValue); 
     echo '<br/>'; 
     } 
    }  
    } 
    echo '<br/><br/>'; 
} 

?> 

应输出;

1 
26.04.2013 
19:43 
Processing 
Jeddah 


2 
26.04.2013 
20:43 
Printed 
RIY 
+0

谢谢我已编辑但没有发生。如果您想查看,我还添加了我的链接。再次感谢 – 2013-04-26 09:41:11

+0

@路易:我编辑了我的代码,试试。 – Dom 2013-04-26 09:47:59

+0

感谢它在添加'file_get_contents'之后现在可以工作。 – 2013-04-26 09:54:55

1

这段代码有几个问题。

加载HTML

$input = 'MyLink'; 
$html = new DOMDocument(); 
$html->loadHTML($input); 

此代码试图把字符串'MyLink'为HTML,这显然是不。如果这是你的实际代码,那么除了这一点,没有什么可以工作。请提供正确的HTML输入或使用loadHTMLFile从文件加载HTML。

比较是区分大小写的

一方面,有这样的:

<tr class='Jed01'> 

,而在另一这样的:

if($tr->getAttribute('class') == 'JED01') 

由于'Jed01' = 'JED01'这个意志!永远不会是true。要么固定外壳,要么使用其他机制(如stricmp)来比较类。

对象不能打印

这将导致一个致命错误:

echo ($td); 

应然相反:最有可能echo $td->nodeValue,但其他可能性是开放取决于你想要做什么。

但是你可以使用XPath

$xpath = new DOMXPath($html); 
$query = "//tr[@class='Jed01']//td[@class='JEDResult']"; // google XPath syntax 

foreach ($xpath->query($query) as $node) { 
    print_r($node->nodeValue); 
} 
+0

感谢您的回答乔恩。我从JEF01编辑Jed01和回声,但没有改变。再次感谢我需要帮助。 – 2013-04-26 09:47:41