https://github.com/amal/cdom
CDom est un simple composant HTML / XML / BBCODE DOM. Il fournit un analyseur pour le langage de balisage de type HTML dans la structure de type DOM et le support de la recherche dans le DOM avec une pleine force des sélecteurs CSS3 et de toute manipulation. Le CDOM est basé sur PHP Simple HTML DOM Parser et sous licence MIT.
Caractéristiques principales et possibilites:
CDOM est écrit pour Anizoptera CMF par Amal Samally (Amal.samally sur gmail.com)
L'utilisation de CDOM est très simple. La plupart des méthodes correspondent à celles de JQuery. Toutes les méthodes sont commentées par détail. Votre IDE peut facilement afficher la complétion automatique, si vous prenez en charge PHPDOC. Et vous pouvez voir des exemples d'utilisation ci-dessous. La documentation complète sera bientôt.
Comment obtenir des éléments HTML?
// Create DOM from string
$ html = file_get_contents ( ' http://www.google.com/ ' );
$ dom = CDom:: fromString ( $ html );
// Find all images
foreach ( $ dom -> find ( ' img ' ) as $ element ) {
echo $ element -> src . "n" ;
}
// Find all links
foreach ( $ dom -> find ( ' a ' ) as $ element ) {
echo $ element -> href . "n" ;
}Comment modifier les éléments HTML?
// Create DOM from string
$ dom = CDom:: fromString ( ' <div id="hello">Hello</div><div id="world">World</div> ' );
// Add class to the second div (first last child)
$ dom -> find ( ' div:nth-last-child(1) ' )-> class = ' bar ' ;
// Change text of first div
$ dom -> find ( ' div[id=hello] ' )-> text ( ' foo ' );
echo $ dom . "n" ; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>Extraire le contenu de HTML
$ html = file_get_contents ( ' http://www.google.com/ ' );
// Dump correctly formatted contents without tags from HTML
echo CDom:: fromString ( $ html )-> text () . "n" ;Utilisez CDOM pour travailler avec simple BBCODE
$ bbMarkup = <<<'TXT'
[quote]
[b]Bold [u]Underline[/u][/b]
[i]Italic
[/quote]
[img width=12 height=16]url[/img]
TXT;
CDom:: $ bracketOpen = ' [ ' ;
CDom:: $ bracketClose = ' ] ' ;
CDom:: $ blockTags = array ( ' quote ' => true );
CDom:: $ inlineTags = array ( ' b ' => true , ' i ' => true , ' u ' => true );
CDom:: $ selfClosingTags = array ();
// Create DOM from string
$ dom = CDom:: fromString ( $ bbMarkup );
// Find [b]
$ b = $ dom -> find ( ' b ' );
$ expected = ' [b]Bold [u]Underline[/u][/b] ' ;
echo $ b -> outerHtml () . "n" ; // Output: [b]Bold [u]Underline[/u][/b]
// Change [img] width
$ img = $ dom -> lastChild ;
$ img -> width = 450 ;
echo $ img -> outerHtml () . "n" ; // Output: [img width="450" height="16"]url[/img]
// Convert [b] to html
CDom:: $ bracketOpen = ' < ' ;
CDom:: $ bracketClose = ' > ' ;
echo $ b -> outerHtml () . "n" ; // Output: <b>Bold <u>Underline</u></b>