PHP code example of joby / html-object-strings

1. Go to this page and download the library: Download joby/html-object-strings library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joby / html-object-strings example snippets


use Joby\HTML\Html5\InlineTextSemantics\ATag;
use Joby\HTML\Html5\TextContentTags\DivTag;
use Joby\HTML\Nodes\Text;

$div = new DivTag();
$div->addChild(
    (new ATag())
        ->setHref('https://example.com')
        ->addChild(new Text('Click here'))
);

echo $div;

use Joby\HTML\Html5\Html5Parser;

$parser = new Html5Parser();

// Parse a fragment into a container of nodes
$fragment = $parser->parseFragment('<div id="foo"><p>Hello</p></div>');

// Parse a full document
$document = $parser->parseDocument('<html><head><title>My Page</title></head><body><div>foo</div></body></html>');
echo $document->html()->head()->title()->content(); // "My Page"

use Joby\HTML\Html5\InlineTextSemantics\ATag;

// Find all links in a parsed fragment and rewrite their hrefs
foreach ($fragment->walk(ATag::class) as $link) {
    $link->setHref('https://proxy.example.com?url=' . urlencode($link->href()));
}

// Walk all nodes without filtering
foreach ($fragment->walk() as $node) {
    echo get_class($node) . PHP_EOL;
}