### Install PHPHtmlParser Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Use Composer to install the package in your project. ```bash $ composer require paquettg/php-html-parser ``` -------------------------------- ### Parse HTML from String Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Basic usage example demonstrating how to load a string and query elements using CSS selectors. ```php // Assuming you installed from Composer: require "vendor/autoload.php"; use PHPHtmlParser\Dom; $dom = new Dom; $dom->loadStr('

Hey bro, click here
:)

'); $a = $dom->find('a')[0]; echo $a->text; // "click here" ``` -------------------------------- ### HTML Meta Tag Example Source: https://github.com/paquettg/php-html-parser/blob/master/tests/data/files/html5.html Example of a meta tag with multiple charset definitions that causes issues with greedy regex. ```html ``` -------------------------------- ### HTML5 Meta Charset Tag Source: https://github.com/paquettg/php-html-parser/blob/master/tests/data/files/html5.html Example of the HTML5 meta charset tag that is currently unsupported by the parser. ```html ``` -------------------------------- ### Configure Parsing Options Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Demonstrates setting global options via setOptions and instance-specific options during the loadFromUrl method. ```php // Assuming you installed from Composer: require "vendor/autoload.php"; use PHPHtmlParser\Dom; use PHPHtmlParser\Options; $dom = new Dom; $dom->setOptions( // this is set as the global option level. (new Options()) ->setStrict(true) ); $dom->loadFromUrl('http://google.com', (new Options())->setWhitespaceTextNode(false) // only applies to this load. ); $dom->loadFromUrl('http://gmail.com'); // will not have whitespaceTextNode set to false. ``` -------------------------------- ### Use Static Facade Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Mounts the static facade to perform DOM operations without manual instantiation. ```php PHPHtmlParser\StaticDom::mount(); Dom::loadFromFile('tests/big.hmtl'); $objects = Dom::find('.content-border'); ``` -------------------------------- ### Load HTML from File Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Load HTML content from a local file and iterate through selected elements. ```php // Assuming you installed from Composer: require "vendor/autoload.php"; use PHPHtmlParser\Dom; $dom = new Dom; $dom->loadFromFile('tests/data/big.html'); $contents = $dom->find('.content-border'); echo count($contents); // 10 foreach ($contents as $content) { // get the class attr $class = $content->getAttribute('class'); // do something with the html $html = $content->innerHtml; // or refine the find some more $child = $content->firstChild(); $sibling = $child->nextSibling(); } ``` -------------------------------- ### Initialize VideoPress and Gravatar Source: https://github.com/paquettg/php-html-parser/blob/master/tests/data/files/index.html Configures VideoPress flash parameters and initializes Gravatar hovercards on document ready. ```javascript if(jQuery.VideoPress){jQuery.VideoPress.video.flash.params.wmode="opaque";} var WPGroHo = {"my_hash":""}; //initialize and attach hovercards to all gravatars jQuery( document ).ready( function( $ ) { if (typeof Gravatar === "undefined"){ return; } if ( typeof Gravatar.init !== "function" ) { return; } Gravatar.profile_cb = function( hash, id ) { WPGroHo.syncProfileData( hash, id ); }; Gravatar.my_hash = WPGroHo.my_hash; Gravatar.init( 'body', '#wp-admin-bar-my-account' ); }); ``` -------------------------------- ### Run PHP-CS-Fixer Source: https://github.com/paquettg/php-html-parser/blob/master/CONTRIBUTING.md Execute this command to automatically format your code according to the project's PSR-12 coding standards. ```bash ./vendor/bin/php-cs-fixer fix ``` -------------------------------- ### Load HTML from URL Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Fetch and parse HTML directly from a URL, with options for custom HTTP clients. ```php // Assuming you installed from Composer: require "vendor/autoload.php"; use PHPHtmlParser\Dom; $dom = new Dom; $dom->loadFromUrl('http://google.com'); $html = $dom->outerHtml; // or $dom->loadFromUrl('http://google.com'); $html = $dom->outerHtml; // same result as the first example ``` ```php // Assuming you installed from Composer: require "vendor/autoload.php"; use PHPHtmlParser\Dom; use App\Services\MyClient; $dom = new Dom; $dom->loadFromUrl('http://google.com', null, new MyClient()); $html = $dom->outerHtml; ``` -------------------------------- ### Dom::loadFromUrl Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Loads and parses HTML content from a URL. ```APIDOC ## Dom::loadFromUrl ### Description Loads HTML content from a specified URL and parses it into the DOM. ### Parameters - **url** (string) - Required - The URL to fetch. - **options** (PHPHtmlParser\Options) - Optional - Instance-specific parsing options. ``` -------------------------------- ### Dom::setOptions Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Sets global parsing options for the Dom object. ```APIDOC ## Dom::setOptions ### Description Sets global configuration options for the parser instance. ### Parameters - **options** (PHPHtmlParser\Options) - Required - An instance of the Options class containing the desired configuration. ``` -------------------------------- ### Load HTML String Directly Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Directly load a string into the DOM parser. ```php // Assuming you installed from Composer: require "vendor/autoload.php"; use PHPHtmlParser\Dom; $dom = new Dom; $dom->loadStr('String'); $html = $dom->outerHtml; ``` -------------------------------- ### Remote Login Message Event Listener Source: https://github.com/paquettg/php-html-parser/blob/master/tests/data/files/mvEregiReplaceFailure.html Handles incoming postMessage events from the remote login origin to manage authentication tokens, redirects, and Safari ITP storage access. ```javascript window.addEventListener( "message", function( e ) { if ( e.origin === "https://r-login.wordpress.com" ) { var data = {}; try { data = JSON.parse( e.data ); } catch( e ) { wpcom_remote_login_final_cleanup(); return; } if ( data.msg === 'LOGIN' ) { // Clean up the login check iframe wpcom_remote_login_remove_dom_node_id( "wpcom_remote_login_key" ); var id_regex = new RegExp( /^[0-9]+$/ ); var token_regex = new RegExp( /^.*|.*|.*$/ ); if ( token_regex.test( data.token ) && id_regex.test( data.wpcomid ) ) { // We have everything we need to ask for a login var script = document.createElement( "script" ); script.setAttribute( "id", "wpcom_remote_login_validate" ); script.src = '/remote-login.php?wpcom_remote_login=validate' + '&wpcomid=' + data.wpcomid + '&token=' + encodeURIComponent( data.token ) + '&host=' + window.location.protocol + '//' + window.location.hostname; document.body.appendChild( script ); } return; } // Safari ITP, not logged in, so redirect if ( data.msg === 'LOGIN-REDIRECT' ) { window.location = 'https://wordpress.com/log-in?redirect_to=' + window.location.href; return; } // Safari ITP, storage access failed, remove the request if ( data.msg === 'LOGIN-REMOVE' ) { var css_zap = 'html { -webkit-transition: margin-top 1s; transition: margin-top 1s; } /* 9001 */ html { margin-top: 0 !important; } * html body { margin-top: 0 !important; } @media screen and ( max-width: 782px ) { html { margin-top: 0 !important; } * html body { margin-top: 0 !important; } }'; var style_zap = document.createElement( 'style' ); style_zap.type = 'text/css'; style_zap.appendChild( document.createTextNode( css_zap ) ); document.body.appendChild( style_zap ); var e = document.getElementById( 'wpcom_request_access_iframe' ); e.parentNode.removeChild( e ); document.cookie = 'wordpress_com_login_access=denied; path=/; max-age=31536000'; return; } // Safari ITP if ( data.msg === 'REQUEST_ACCESS' ) { console.log( 'request access: safari' ); // Check ITP iframe enable/disable knob if ( wpcom_remote_login_extra_auth !== 'safari_itp_iframe' ) { return; } // If we are in a "private window" there is no ITP. var private_window = false; try { var opendb = window.openDatabase( null, null, null, null ); } catch( e ) { private_window = true; } if ( private_window ) { console.log( 'private window' ); return; } var iframe = document.createElement( 'iframe' ); iframe.id = 'wpcom_request_access_iframe'; iframe.setAttribute( 'scrolling', 'no' ); iframe.setAttribute( 'sandbox', 'allow-storage-access-by-user-activation allow-scripts allow-same-origin allow-top-navigation-by-user-activation' ); iframe.src = 'https://r-login.wordpress.com/remote-login.php?wpcom_remote_login=request_access&origin=' + encodeURIComponent( data.origin ) + '&wpcomid=' + encodeURIComponent( data.wpcomid ); var css = 'html { -webkit-transition: margin-top 1s; transition: margin-top 1s; } /* 9001 */ html { margin-top: 46px !important; } * html body { margin-top: 46px !important; } @media screen and ( max-width: 660px ) { html { margin-top: 71px !important; } * html body { margin-top: 71px !important; } #wpcom_request_access_iframe { display: block; height: 71px !important; } } #wpcom_request_access_iframe { border: 0px; height: 46px; position: fixed; top: 0; left: 0; width: 100%; min-width: 100%; z-index: 99999; background: #23282d; } '; var style = document.createElement( 'style' ); style.type = 'text/css'; style.id = 'wpcom_request_access_styles'; style.appendChild( document.createTextNode( css ) ); document.body.appendChild( style ); document.body.appendChild( iframe ); } } } ); ``` -------------------------------- ### Original Greedy Regex Charset Detection Source: https://github.com/paquettg/php-html-parser/blob/master/tests/data/files/html5.html The original implementation of charset detection using a greedy regex that fails on multiple charset definitions. ```php $matches = []; if (preg_match('/charset=(.+)/', $content, $matches)) { $encode->from(trim($matches[1])); $this->root->propagateEncoding($encode); return true; } ``` -------------------------------- ### Improved Non-Greedy Regex Charset Detection Source: https://github.com/paquettg/php-html-parser/blob/master/tests/data/files/html5.html The corrected regex implementation using a semicolon as a terminator for the charset identifier. ```php if (preg_match('/charset=([^;]+)/', $content, $matches)) { ``` -------------------------------- ### DOM Manipulation for Remote Login Cleanup Source: https://github.com/paquettg/php-html-parser/blob/master/tests/data/files/mvEregiReplaceFailure.html Utility functions to remove specific DOM elements by ID or class name during the login cleanup process. ```javascript function wpcom_remote_login_remove_dom_node_id( element_id ) { var dom_node = document.getElementById( element_id ); if ( dom_node ) { dom_node.remove(); } } function wpcom_remote_login_remove_dom_node_classes( class_name ) { var dom_nodes = document.querySelectorAll( '.' + class_name ); for ( var i = 0; i < dom_nodes.length; i++ ) { dom_nodes[i].remove(); } } function wpcom_remote_login_final_cleanup() { wpcom_remote_login_remove_dom_node_classes( "wpcom_remote_login_msg" ); wpcom_remote_login_remove_dom_node_id( "wpcom_remote_login_key" ); wpcom_remote_login_remove_dom_node_id( "wpcom_remote_login_validate" ); wpcom_remote_login_remove_dom_node_id( "wpcom_remote_login_js" ); wpcom_remote_login_remove_dom_node_id( "wpcom_request_access_iframe" ); wpcom_remote_login_remove_dom_node_id( "wpcom_request_access_styles" ); } ``` -------------------------------- ### Dom::delete Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Removes a node from the DOM tree. ```APIDOC ## Dom::delete ### Description Removes the current node from the DOM tree structure. It is recommended to unset the variable after calling this method to free memory. ``` -------------------------------- ### Modify TextNode content Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Use this approach to update the text within a specific node. Ensure the node is correctly identified before calling setText. ```php use PHPHtmlParser\Dom; $dom = new Dom; $dom->loadStr('

Hey bro, click here
:)

'); /** @var Dom\Node\InnerNode $a */ $a = $dom->find('a')[0]; $a->firstChild()->setText('biz baz'); echo $dom; // '

Hey bro, biz baz
:)

' ``` -------------------------------- ### Dom::setAttribute Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Modifies an attribute of a specific DOM node. ```APIDOC ## Dom::setAttribute ### Description Updates or sets an attribute on a node within the DOM tree. ### Parameters - **attribute** (string) - Required - The name of the attribute. - **value** (string) - Required - The value to assign to the attribute. ``` -------------------------------- ### Delete Node from Tree Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Removes a node from the DOM tree and unsets the variable to free memory. ```php use PHPHtmlParser\Dom; $dom = new Dom; $dom->loadStr('

Hey bro, click here
:)

'); /** @var Dom\Node\AbstractNode $a */ $a = $dom->find('a')[0]; $a->delete(); unset($a); echo $dom; // '

Hey bro,
:)

'); ``` -------------------------------- ### Modify Node Attributes Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Updates an attribute of a specific node using the setAttribute method. ```php use PHPHtmlParser\Dom; $dom = new Dom; $dom->loadStr('

Hey bro, click here
:)

'); $a = $dom->find('a')[0]; $a->setAttribute('class', 'foo'); echo $a->getAttribute('class'); // "foo" ``` -------------------------------- ### Manipulate Tag Object Source: https://github.com/paquettg/php-html-parser/blob/master/README.md Accesses the underlying Tag object to modify node attributes directly. ```php use PHPHtmlParser\Dom; $dom = new Dom; $dom->loadStr('

Hey bro, click here
:)

'); /** @var Dom\Node\AbstractNode $a */ $a = $dom->find('a')[0]; $tag = $a->getTag(); $tag->setAttribute('class', 'foo'); echo $a->getAttribute('class'); // "foo" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.