1

单击 A.html 中的链接时:

<tr>
<td class="book"><a class="booklink" ref="../collection?file=book1.pdf">
Good Read
</a>
-Blah blah blah
</td>  
</tr>

?file=book1.pdf 被传递给 B.html:

<?php
$src = $_GET['file'];
?>

<iframe src="<?php echo $src; ?>" >
</iframe>

问题:- 如何从 A.html 中检索文本“Good Read-Blah blah blah”,并使用简单的 html dom 将其粘贴到 B.html 中的元描述中?(请知道A.html中的表格有上千条列出的数据)

谢谢你。

4

1 回答 1

0

使用DOM加载您的 HTML 文档并使用XPath 来搜索它

// note: if the HTML to parse has bad syntax, use: libxml_use_internal_errors(true);

$doc = new DOMDocument;
$doc->loadHTML(file_get_contents('A.html'));
if ($doc === false) {
    throw new RuntimeException('Could not load HTML');
}

$xpath = new DOMXPath($doc);
$xpathResult = $xpath->query("//a[@href = '../collection?file={$_GET['file']}']/..");
if ($xpathResult === false) {
    throw new LogicException('Something went wrong querying the document!');
}

foreach ($xpathResult as $domNode) {
    echo 'Link text: ' . htmlentities($domNode->textContent) . PHP_EOL;
}
于 2013-08-10T01:37:49.517 回答