gpt4 book ai didi

php - 从 PHP 获取/过滤表行内容

转载 作者:行者123 更新时间:2023-12-02 19:51:55 25 4
gpt4 key购买 nike

如何获取从文件中读取的所有标题和链接?

以下文件的示例内容

<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>

我试过了

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

但没有得到我真正想要的数据!

最佳答案

例如这样

$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 ($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
}

或者你需要他们分组吗?

关于php - 从 PHP 获取/过滤表行内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9170658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com