Php-mysql Course

getElementsByTagName()

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).
- It is indicated to have HTML well-formed, otherways may generate E_WARNING errors when it encounters bad markup.

To traverse the elements of a PHP object, use the foreach() loop instruction.

getElementById

The getElementById('ID') function returns an object that contains the element with a given ID, or NULL if the element is not found.
This function is useful when you want to read the content, or attribute value of a HTML element with a specified ID.
    - Use the nodeValue property to get the content of the element returned by getElementById().
    - Use the tagName (or nodeName) property to get the name of the tag.

Example, gets the tag name and content of the element with a specified ID:
<?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
?>

getElementsByTagName

The getElementsByTagName('tag') function returns an object that contains all the elements with a given local tag name. The special argument '*' matches all tags.
This function is useful when you want to read the content, or attribute of multiple HTML elements that have the same <tag>.
    - Use the getAttribute('attribute') to get the value of a specified attribute.

Example, gets, and outputs the ID and content of each DIV:
<?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/>';
}
?>

- You also can use /load only a part of the HTML document.
Example 2. Loads a string containing only the BODY part, store into an Array the content of each paragraph with class="cls".
<?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 )
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
PHP getElementById and getElementsByTagName

Last accessed pages

  1. Simple Admin Login PHP Script (10894)
  2. jQuery parent, children and nth-child() (13826)
  3. Display UL bullets and OL numbers on the right side (8088)
  4. Convert BBCode to HTML and HTML to BBCode with JavaScript (9298)
  5. jsSHA - SHA Hashes and HMAC in JavaScript (3445)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (201)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide