Php-mysql Course

hasAttribute
setAttribute
removeAttribute

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.

getAttribute

The getAttribute('attr') function returns the value of the specified attribute, or an empty string if no attribute with the given "attr" is found.
- This method must be applied to an object that represents a HTML (or XML) element into a DOMDocument object.
    - You can use the getElementsByTagName('tag') to get the elements with a specified <tag>.
    - To get the element with a specified ID, use the getElementById('ID') method.

Example, gets the "href" value of each <a> in #menu:
<?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/>';
}
?>

Results:
https://coursesweb.net/
https://marplo.net/
http://www.php.net/

hasAttribute

The hasAttribute('attr') function returns TRUE if the given attribute exists, otherwise FALSE.
- This method is useful when you want to get the value of a specified attribute; to avoid the errors, you can check if that attribute exists.
- You also can use /load only a part of the HTML document.

Example. Loads a string containing only the BODY part, outputs the ID, and content of DIVs that have ID attribute:
<?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/>';
  }
}
?>

Results:
dv1 - Free PHP - MySQL Course.
did - Web Development resources.

setAttribute

The setAttribute('name', 'value') function sets an attribute "name" to the given "value". If the attribute does not exist, it will be created.
- This method is useful when you want to create an attribute into a tag, or to set other value to the existing attribute.
    - Use the $doc->saveHTML() method to put into a string a DOMDocument node with HTML content. Usualy needed after you make changes in the DOMDocument object.
    - This code:
$dochtml->getElementsByTagName('body')->item(0)
returns a DOMDocument node with <body> element. It is useful when you want to work only with the <body> item (including all its content), for example to get a string with BODY content.

Example, sets class="newcls" to all paragraphs:
<?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;
?>

Results:
<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>

removeAttribute

The removeAttribute('attr') function removes attribute named "attr" from the element. Returns TRUE on success or FALSE on failure.
Example, delete the "class" attribute from each <p> tag with class="cls":
<?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;
?>

Outputs:
<body>
 <p>Free PHP - MySQL Lessons.</p>
 <p class="othercls">Web Programming courses.</p>
</body>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Working with HTML attributes in PHP

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141749)
  2. Node.js Move and Copy file (28420)
  3. MouseEvent - Events for Mouse (2909)
  4. PHPMailer (2311)
  5. Uploading images to server with Ajax (6095)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (473)
  2. CSS cursor property - Custom Cursors (79)
  3. The Mastery of Love (70)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)