2009-09-01 44 views
27

我很好奇PHP是否存在用于处理HTML/XML文件的jQuery风格的接口/库 - 特别是使用jQuery风格selectors用于PHP的类似jQuery的界面?

我想要做这样的事情(所有假设):

 
foreach (j("div > p > a") as anchor) { 
    // ... 
} 


print j("#some_id")->html(); 


print j("a")->eq(0)->attr("name"); 

这些只是几个例子。

我做了尽可能多的谷歌搜索,但无法找到我正在寻找的东西。有谁知道,如果沿着这些线存在,或者这是我将不得不使用domxml从头开始做的事情?

+2

你真的在寻找CSS3和XPATH风格的选择器。 JQuery和其他JS库基于那些标准化的XML选择器。 – bucabay 2009-09-01 20:49:08

+0

最新的jQuery for PHP是https://github.com/technosophos/querypath – Petah 2012-03-29 04:26:12

回答

22

做一些更多的狩猎,我想我可能已经找到正是我寻找:

phpQuery - jQuery port to PHP

谢谢大家的回答,我一定会记住他们的其他用途。

+0

如果phpQuery回答你的问题,你应该选择这个答案。 – karim79 2009-09-01 19:54:05

+0

我已经尝试过,我需要等待2天,直到我明显地接受我自己的答案。 – theotherlight 2009-09-01 20:05:45

+3

嘿,我可以问你怎么决定phpQuery和QueryPath?我想选择一个。 – Webinan 2013-12-31 15:18:12

0

simplexml也许?它的语法与jquery不同,但它确实使得遍历XML非常容易。

然而,它不适用于HTML无效的XML。

30

PHP Simple HTML DOM Parser使用jQuery风格的选择器。

修改HTML元素:从documentation例子

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

刮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); 
2

我写了一个库,它在PHP中复制jQuery的DOM操作方法,但它使用xpath,而不是jquery样式选择器。否则,它的作用几乎相同。

[http://pxtreme.sourceforge.net] [1]

$doc = px("index.html"); // Create a px Object 
$headings=$doc->xpath("/html/body/h2"); // Select Elements to Manipulate 
$headings->addClass("NewLook"); // Change their Appearance 
px("index.html")->xpath("//h2")->addClass("NewLook"); // All in One Line 

// use anonymous functions in PHP 5.3 
$doc->xpath("//p")->each(function ($pxObject, $index) { 
    $str = $pxObject->get($index)->text(); 
    if (mb_strpos($str, "pxtreme")) 
    $px->attr("title", "Check out this paragraph!"); 
}); 

http://pxtreme.sourceforge.net

12

的问题是旧的,但你需要的是Query Path

+0

对于PHP来说,这是最好的,也是最新的jQuery! – Petah 2012-03-29 04:25:21

+2

这绝对是一个很好的解决方案,它不需要做任何事情来完成PHP安装 – Eva 2012-08-04 06:14:44

5

相信我你正在寻找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); 
} 
?> 
2

HtmlPageDom扩展了Symfony的DOM Crawler,并增加了类似于jQuery的DOM操作函数。

+0

这个更有意义,因为它使用更新的组件读取DOM。 – kzap 2015-10-02 00:44:12

+0

这很好 – 2016-07-31 19:43:52

1

最好的一个,我发现是https://github.com/scotteh/php-dom-wrapper

它的工作原理非常类似于jQuery和它的速度很快。

我尝试了很多来自其他答案的库,但我没有设法移植我在jQuery中轻松完成的操作。有了这个,它变得轻而易举。我想它会很快变得更受欢迎...

相关问题