/README.md

https://github.com/amal/CDom · Markdown · 118 lines · 84 code · 34 blank · 0 comment · 0 complexity · d7bed4c4166b8c5946e72e9a0bb3895c MD5 · raw file

  1. CDom
  2. ====
  3. https://github.com/amal/CDom
  4. CDom is a simple HTML/XML/BBCode DOM component.
  5. It provides a parser for HTML-like markup language in the DOM-like structure and support searching through the DOM with full strength of CSS3 selectors and any manipulations.
  6. CDom is based on [PHP Simple HTML DOM Parser](http://simplehtmldom.sourceforge.net/) and licensed under the MIT License.
  7. Main features and possibilites:
  8. * Traversing and manipulations in jQuery-like manner for PHP.
  9. * Automatic detection of encoding.
  10. * Supports damaged and malformed html.
  11. * Full support of [CSS3 selectors](http://www.w3.org/TR/css3-selectors/).
  12. * Support of [jQuery selector extensions](http://api.jquery.com/category/selectors/jquery-selector-extensions/).
  13. * Extract contents from DOM as text or html in a single line.
  14. * Full code coverage.
  15. * Can work with simple BBCode or other HTML-like markup languages.
  16. CDom is written for Anizoptera CMF by Amal Samally (amal.samally at gmail.com)
  17. Documentation
  18. -------------
  19. CDom use is very simple. Most of the methods match the jQuery's ones. All methods are detail commented. Your IDE can easy show autocompletion, if support PHPDoc. And you can see examples of using below.
  20. Full documentation will be soon.
  21. Requirements
  22. ------------
  23. * CDom requires PHP 5.3.0 (or later).
  24. Examples
  25. --------
  26. How to get HTML elements?
  27. ```php
  28. // Create DOM from string
  29. $html = file_get_contents('http://www.google.com/');
  30. $dom = CDom::fromString($html);
  31. // Find all images
  32. foreach($dom->find('img') as $element) {
  33. echo $element->src . "\n";
  34. }
  35. // Find all links
  36. foreach($dom->find('a') as $element) {
  37. echo $element->href . "\n";
  38. }
  39. ```
  40. How to modify HTML elements?
  41. ```php
  42. // Create DOM from string
  43. $dom = CDom::fromString('<div id="hello">Hello</div><div id="world">World</div>');
  44. // Add class to the second div (first last child)
  45. $dom->find('div:nth-last-child(1)')->class = 'bar';
  46. // Change text of first div
  47. $dom->find('div[id=hello]')->text('foo');
  48. echo $dom . "\n"; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
  49. ```
  50. Extract contents from HTML
  51. ```php
  52. $html = file_get_contents('http://www.google.com/');
  53. // Dump correctly formatted contents without tags from HTML
  54. echo CDom::fromString($html)->text() . "\n";
  55. ```
  56. Use CDom to work with simple BBCode
  57. ```php
  58. $bbMarkup = <<<'TXT'
  59. [quote]
  60. [b]Bold [u]Underline[/u][/b]
  61. [i]Italic
  62. [/quote]
  63. [img width=12 height=16]url[/img]
  64. TXT;
  65. CDom::$bracketOpen = '[';
  66. CDom::$bracketClose = ']';
  67. CDom::$blockTags = array('quote' => true);
  68. CDom::$inlineTags = array('b' => true, 'i' => true, 'u' => true);
  69. CDom::$selfClosingTags = array();
  70. // Create DOM from string
  71. $dom = CDom::fromString($bbMarkup);
  72. // Find [b]
  73. $b = $dom->find('b');
  74. $expected = '[b]Bold [u]Underline[/u][/b]';
  75. echo $b->outerHtml() . "\n"; // Output: [b]Bold [u]Underline[/u][/b]
  76. // Change [img] width
  77. $img = $dom->lastChild;
  78. $img->width = 450;
  79. echo $img->outerHtml() . "\n"; // Output: [img width="450" height="16"]url[/img]
  80. // Convert [b] to html
  81. CDom::$bracketOpen = '<';
  82. CDom::$bracketClose = '>';
  83. echo $b->outerHtml() . "\n"; // Output: <b>Bold <u>Underline</u></b>
  84. ```