Coder Social home page Coder Social logo

cdom's Introduction

CDom

https://github.com/amal/CDom

CDom is a simple HTML/XML/BBCode DOM component. 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. CDom is based on PHP Simple HTML DOM Parser and licensed under the MIT License.

Main features and possibilites:

  • Traversing and manipulations in jQuery-like manner for PHP.
  • Automatic detection of encoding.
  • Supports damaged and malformed html.
  • Full support of CSS3 selectors.
  • Support of jQuery selector extensions.
  • Extract contents from DOM as text or html in a single line.
  • Full code coverage.
  • Can work with simple BBCode or other HTML-like markup languages.

CDom is written for Anizoptera CMF by Amal Samally (amal.samally at gmail.com)

Documentation

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. Full documentation will be soon.

Requirements

  • CDom requires PHP 5.3.0 (or later).

Examples

How to get HTML elements?

// 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";
}

How to modify HTML elements?

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

Extract contents from 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 to work with 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>

cdom's People

Contributors

amal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cdom's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.