The PHP DOMElement class contains methods that can be used to read, set, and remove attributes in a HTML document loaded into a DOMDocument object.
To traverse the elements of a PHP object, use the foreach() loop instruction.
<?php // string with HTML content $strhtml = '<!doctype html> <html> <head> <meta charset="utf-8" /> <title>PHP getAttribute</title> </head> <body> <ul id="menu"> <li><a href="https://coursesweb.net/" title="Web Programming Course">CoursesWeb.net</a></li> <li><a href="https://marplo.net/" title="Free Courses, Games, Anime">MarPlo.net</a></li> <li><a href="http://www.php.net/" title="PHP Website">php.net</a></li> </ul> </body></html>'; // create the DOMDocument object, and load HTML from string $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); // gets the element with id="menu" $menu = $dochtml->getElementById('menu'); // gets all <a> tags in $menu $atgs = $menu->getElementsByTagName('a'); // traverse the object with all <a> in $menu foreach($atgs as $atag) { // outputs the "href" value echo $atag->getAttribute('href'). '<br/>'; } ?>
<?php // string with HTML content $strhtml = '<body> <div id="dv1">Free PHP - MySQL Course.</div> <div>Working with HTML attributes in PHP.</div> <div id="did">Web Development resources.</div> </body>'; // create the DOMDocument object, and load HTML from string $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); // gets all DIVs $divs = $dochtml->getElementsByTagName('div'); // traverse the object with all DIVs foreach($divs as $div) { // if the current $div has ID attribute, gets and outputs the ID and content if($div->hasAttribute('id')) { $id = $div->getAttribute('id'); $cnt = $div->nodeValue; echo $id. ' - '. $cnt. '<br/>'; } } ?>
<?php // string with HTML content $strhtml = '<!doctype html> <html> <head> <meta charset="utf-8" /> <title>PHP setAttribute</title> </head> <body> <p class="cls">Free PHP - MySQL Lessons.</p> <p>URL: https://coursesweb.net/php-mysql/</p> <p>Web Development resources.</p> </body></html>'; // create the DOMDocument object, and load HTML from string $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); // gets all paragraphs $prgs = $dochtml->getElementsByTagName('p'); // traverse the object with all paragraphs foreach($prgs as $prg) { // set /create class="newcls" to each <p> $prg->setAttribute('class', 'newcls'); } // gets an object with <body> element (including all its content) $body = $dochtml->getElementsByTagName('body')->item(0); // adds the $body content into a string, and outputs it $strbody = $dochtml->saveHTML($body); echo $strbody; ?>
<body> <p class="newcls">Free PHP - MySQL Lessons.</p> <p class="newcls">URL: https://coursesweb.net/php-mysql/</p> <p class="newcls">Web Development resources.</p> </body>
<?php // string with HTML content $strhtml = '<body> <p class="cls">Free PHP - MySQL Lessons.</p> <p class="othercls">Web Programming courses.</p> </body>'; // create the DOMDocument object, and load HTML from string $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); // gets all paragraphs $prgs = $dochtml->getElementsByTagName('p'); // traverse the object with all paragraphs foreach($prgs as $prg) { // if the current item has class="cls", removes it if($prg->hasAttribute('class') && $prg->getAttribute('class') == 'cls') { $prg->removeAttribute('class'); } } // gets an object with <body> element (including all its content) $body = $dochtml->getElementsByTagName('body')->item(0); // adds the $body content into a string, and outputs it $strbody = $dochtml->saveHTML($body); echo $strbody; ?>
<body> <p>Free PHP - MySQL Lessons.</p> <p class="othercls">Web Programming courses.</p> </body>
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);