https://github.com/amal/cdom
CDOM es un componente DOM HTML/XML/BBCode simple. Proporciona un analizador para el lenguaje de marcado similar a HTML en la estructura similar a DOM y el apoyo a la búsqueda a través del DOM con plena fuerza de selectores CSS3 y cualquier manipulación. CDOM se basa en PHP Simple HTML DOM PARSER y con licencia bajo la licencia MIT.
Características principales y posibilidades:
CDOM está escrito para Anizoptera CMF por Amal Samally (Amal.samally en Gmail.com)
El uso de CDOM es muy simple. La mayoría de los métodos coinciden con los jQuery. Todos los métodos se comentan detalles. Su IDE puede mostrar fácilmente el autocompleto, si admite PHPDOC. Y puede ver ejemplos de uso a continuación. La documentación completa será pronto.
¿Cómo obtener elementos 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" ;
}¿Cómo modificar los elementos 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>Extraer contenido de HTML
$ html = file_get_contents ( ' http://www.google.com/ ' );
// Dump correctly formatted contents without tags from HTML
echo CDom:: fromString ( $ html )-> text () . "n" ;Use CDOM para trabajar con BBCode simple
$ 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>