简体   繁体   English

从PHP获取/过滤表行内容

[英]Get/filter table-row content from PHP

How do I get all the TITLE and LINKS reading from a file ? 如何从文件中读取所有TITLE和LINKS?

A sample content of the file below 以下文件的示例内容

<tr class="odd">
 <td align="left" valign="top" class="text_cont_normal"> TITLE </td>
 <td align="left" valign="top" class="normal_text_link">
  <img border="0" onclick="javascript:window.location.href='LINK'" style="cursor: pointer;" alt="Download"  src="btn.jpg"/></td>
</tr>
<tr class="even">
 <td align="left" valign="top" class="text_cont_normal"> TITLE2 </td>
 <td align="left" valign="top" class="normal_text_link">
  <img border="0" onclick="javascript:window.location.href='LINK2'" style="cursor: pointer;" alt="Download"  src="btn.jpg"/></td>
</tr>

I tried 我试过了

$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
 if ($tag->hasAttribute('onclick'))
    echo $tag->getAttribute('onclick').'<br>';
}

But not getting the data which I actually want ! 但是没有得到我真正想要的数据!

Like this, for example 例如,像这样

$doc = new DOMDocument();
$doc->loadHTMLFile($filename);
$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//td[@class="text_cont_normal"]');
 foreach($nodes as $node)
 {
    echo $node->nodeValue.'<br>';   // title
 }
$nodes = $xpath->query('//td[@class="normal_text_link"]/img[@alt="Download"]');
 foreach($nodes as $node)
 {
  if ($node->hasAttribute('onclick'))
     echo $node->getAttribute('onclick').'<br>';  //click
 }

If you need exactly the LINK then rewrite 如果你需要完全LINK然后重写

  if ($node->hasAttribute('onclick'))
  {
      echo $node->getAttribute('onclick').'<br>';  //click
      preg_match('/location\.href=(\'|")(.*?)\\1/i', 
                 $node->getAttribute('onclick'), $matches);
      if (isset($matches[2])) echo $matches[2].'<br>'; // the value
  }

Or do you need them in groups? 或者你是否需要分组?

One possible way: 一种可能的方式:

$nodes = $doc->getElementsByTagName('tr');
$max = $nodes->length;
for ($i = 0; $i < $max; $i++)
{
    echo $nodes->item($i)->firstChild->nodeValue . '<br>';  // TITLE
    $onclick = $nodes->item($i)->childNodes->item(2)->childNodes->item(1)->getAttribute('onclick');
    $parts = explode("'", $onclick);
    echo $parts[1] . '<br>';  // LINK
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM