<?php

/* Craur#createFromJson */
$node = Craur::createFromJson('{"book": {"authors": ["Hans", "Paul"]}}');
$authors = $node->get('book.authors[]');
assert(count($authors) == 2);
/* Craur#createFromXml */
$node = Craur::createFromXml('<book><author>Hans</author><author>Paul</author></book>');
$authors = $node->get('book.author[]');
assert(count($authors) == 2);
/* Craur#createFromHtml */
$node = Craur::createFromHtml('<html><head><title>Hans</title></head><body>Paul</body></html>');
assert($node->get('html.head.title') == 'Hans');
assert($node->get('html.body') == 'Paul');
/* Craur#createFromCsvFile */
// If the file loooks like this:
// Book Name;Book Year;Author Name
// My Book;2012;Hans
// My Book;2012;Paul
// My second Book;2010;Erwin
$shelf = Craur::createFromCsvFile('fixtures/books.csv', array('book[].name', 'book[].year', 'book[].author[].name'));
assert(count($shelf->get('book[]')) === 2);
foreach ($shelf->get('book[]') as $book) {
    assert(in_array($book->get('name'), array('My Book', 'My second Book')));
    foreach ($book->get('author[]') as $author) {
        assert(in_array($author->get('name'), array('Hans', 'Paul', 'Erwin')));
    }
}
/* Craur#createFromExcelFile */
// If the file loooks like this:
// Book Name;Book Year;Author Name
Beispiel #2
0
<?php

$craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>HTML5 Test</title></head><body><aside>A sidebar?</aside></body></html>');
assert($craur->get('html.head.title') == 'HTML5 Test');
assert($craur->get('html.body.aside') == 'A sidebar?');
try {
    $craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>HTML5 Test</title></head><body><aside>A sidebar?</aside></body><html>');
    assert(false);
} catch (Exception $exception) {
    assert(strpos($exception->getMessage(), 'Invalid html') > -1);
}
try {
    $craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>HTML5 Test</title></head><body><aside>A sidebar?</aside><img></img></body></html>');
    assert(false);
} catch (Exception $exception) {
    assert(strpos($exception->getMessage(), 'Unexpected end tag : img') > -1);
}
try {
    $craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>HTML5 Test</title></head><body><img></img><aside>A sidebar?</aside></body></html>');
    assert(false);
} catch (Exception $exception) {
    assert(strpos($exception->getMessage(), 'Unexpected end tag : img') > -1);
}
<?php

$invalid_html = file_get_contents(dirname(__FILE__) . '/fixtures/invalid_character_in_html.html');
$craur = Craur::createFromHtml($invalid_html, 'iso-8859-1');
assert($craur->get('html.head.title') == 'xx');
Beispiel #4
0
<?php

$craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>Test Title</title></head><body></body></html>');
assert($craur->get('html.head.title') == 'Test Title');
$craur = Craur::createFromHtml(file_get_contents(dirname(__FILE__) . '/fixtures/strict_html_file.html'));
assert($craur->get('html.head.title') == 'Test Title');
assert($craur->get('html.@xmlns:atom') == 'http://www.w3.org/2005/Atom');
assert($craur->get('html.body.p.img.@width') == '20');
assert($craur->get('html.body.p.img.@height') == '30');
assert($craur->get('html.body.p.img.@src') == 'http://example.org/image.png');
assert($craur->get('html.body.p') == 'testtest2');
try {
    $craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>Test Title</title></head><body></body><html>');
    assert(false);
} catch (Exception $exception) {
    assert(strpos($exception->getMessage(), 'Invalid html') > -1);
}
<?php

$craur = Craur::createFromHtml('<a href="http://dracoblue.net">© by … dracoblue</a>');
assert($craur->get('a') == '© by … dracoblue');
assert($craur->get('a.@href') == 'http://dracoblue.net');