getElementById and getElementsByTagName are methods of the PHP DOMDocument class. These methods can be used in PHP to get elements from a HTML document.
- Before to use the methods of the PHP DOMDocument class, you must load the HTML document into a DOMDocument object, like in this code:
// create the DOMDocument object $dochtml = new DOMDocument(); // load content from a HTML page (or file) $dochtml->loadHTMLFile('filename.html'); // OR, load the HTML items from a string containing the HTML document $strhtml = '<html><body>Tags and content.<br></body></html>'; $dochtml->loadHTML($strhtml);- The $dochtml contains an object with a tree structure of all elements in a HTML document. After this object is created, you can use the DOMDocument methods to access the HTML items (as you can see in the examples below).
To traverse the elements of a PHP object, use the foreach() loop instruction.
<?php $strhtml = '<!doctype html> <html> <head> <meta charset="utf-8" /> <title>PHP getElementById, getElementsByTagName</title> </head> <body> <div id="dv1">https://coursesweb.net</div> </body></html>'; // create the DOMDocument object, and load HTML from a string $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); // get the element with id="dv1" $elm = $dochtml->getElementById('dv1'); // get the tag name, and content $tag = $elm->tagName; $cnt = $elm->nodeValue; echo $tag. ' - '. $cnt; // div - https://coursesweb.net ?>
<?php $strhtml = '<!doctype html> <html> <head> <meta charset="utf-8" /> <title>PHP getElementById, getElementsByTagName</title> </head> <body> <div id="cweb">https://coursesweb.net</div> <p>Free PHP Course</p> <div id="mp">marplo.net</div> </body></html>'; // create the DOMDocument object, and load HTML from a string $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); // gets all DIVs $divs = $dochtml->getElementsByTagName('div'); // traverse the object with all DIVs foreach($divs as $div) { // gets, and outputs the ID and content of each DIV $id = $div->getAttribute('id'); $cnt = $div->nodeValue; echo $id. ' - '. $cnt. '<br/>'; } ?>
<?php $strhtml = '<body> <p class="cls">Free PHP Course</p> <p class="cls">URL: https://coursesweb.net</p> <p>Paragraph without class.</p> <div>marplo.net</div> <p class="cls">PHP getElementById and getElementsByTagName</p> </body>'; // create the DOMDocument object, and load HTML from a string $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); // gets all <p> tags $prgs = $dochtml->getElementsByTagName('p'); $pcls = array(); // traverse the object with all paragraphs foreach($prgs as $prg) { // if the current paragraph has class="cls", adds it in the $pcls array if($prg->getAttribute('class') == 'cls') { $pcls[] = $prg->nodeValue; } } // outputs the $pcls array print_r($pcls); // Array ([0] => Free PHP Course [1] => URL: https://coursesweb.net [2] => PHP getElementById and getElementsByTagName ) ?>
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net