jQuery-like interface for PHP?

前端 未结 12 1695
猫巷女王i
猫巷女王i 2020-12-04 13:44

I was curious as to whether or not there exists a jQuery-style interface/library for PHP for handling HTML/XML files -- specifically using jQuery style selectors.

I\

相关标签:
12条回答
  • 2020-12-04 14:04

    Have you looked into using PHP's DOMDocument class?

    http://us2.php.net/manual/en/book.dom.php

    Not sure if this is exactly what you're looking for, but it does allow for searching a document by various attributes, and other such DOM manipulation.

    0 讨论(0)
  • 2020-12-04 14:10

    PHP Simple HTML DOM Parser uses jQuery-style selectors. Examples from the documentation:

    Modifying HTML elements:

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

    Scraping 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);
    
    0 讨论(0)
  • 2020-12-04 14:10

    The question is old but what you need is Query Path.

    0 讨论(0)
  • 2020-12-04 14:12

    I wrote a library that duplicates jQuery's DOM manipulation methods in PHP, but it uses xpath, not the jquery style selectors. Otherwise, it works pretty much the same.

    [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

    0 讨论(0)
  • 2020-12-04 14:12

    If you use a modern framework, you should check these out too.

    • http://symfony.com/doc/current/components/dom_crawler.html
    • http://symfony.com/doc/current/components/css_selector.html

    These components can be installed via composer.

    0 讨论(0)
  • 2020-12-04 14:13

    Trust me you are looking for xPath. I am showing you an example

    <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);
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题