the*_*ght 31 html php xml jquery html-parsing
我很好奇是否存在用于处理HTML/XML文件的PHP的jQuery样式接口/库 - 特别是使用jQuery样式选择器.
我想做这样的事情(所有假设):
foreach (j("div > p > a") as anchor) {
// ...
}
print j("#some_id")->html();
print j("a")->eq(0)->attr("name");
这只是几个例子.
我尽可能多地使用谷歌搜索,但找不到我想要的东西.有没有人知道这些东西是否存在,或者这是我将要使用domxml从头开始制作的东西?
kar*_*m79 35
PHP Simple HTML DOM Parser使用jQuery样式的选择器.文档中的示例:
修改HTML元素:
// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');
$html->find('div', 1)->class = 'bar';
$html->find('div[id=hello]', 0)->innertext = 'foo';
echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
Run Code Online (Sandbox Code Playgroud)
刮刮Slashdot:
// Create DOM from URL
$html = file_get_html('http://slashdot.org/');
// Find all article blocks
foreach($html->find('div.article') as $article) {
$item['title'] = $article->find('div.title', 0)->plaintext;
$item['intro'] = $article->find('div.intro', 0)->plaintext;
$item['details'] = $article->find('div.details', 0)->plaintext;
$articles[] = $item;
}
print_r($articles);
Run Code Online (Sandbox Code Playgroud)
the*_*ght 24
做一些狩猎,我想我可能已经找到了我正在寻找的东西:
感谢大家的回答,我一定会记住它们用于其他用途.
相信我,你正在寻找xPath.我给你看一个例子
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
$dom = new DOMDocument;
libxml_use_internal_errors(TRUE);
$dom->loadHTMLFile('http://somewhereinblog.net');
libxml_clear_errors();
$xPath = new DOMXPath($dom);
$links = $xPath->query('//h1//a'); //This is xPath. Really nice and better than anything
foreach($links as $link) {
printf("<p><a href='%s'>%s</a></p>\n", $link->getAttribute('href'), $link->nodeValue);
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19530 次 |
| 最近记录: |