DOM

Extension of PHP DOM implementation.

Functions

DOM::createDom

Definition

Name
createDom
Syntax
DOMElement = DOM::createDom( String $html_string );

Description

Create a DOM from string of HTML.

Parameters

$html_string
String String to using in document body.

Return values

DOMElement Native PHP DOM element

Examples

DOM()->createDom("hello world");

Returns a DOM object with the following structure:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <span>Hello world</span> </body> </html>

Dependencies

PHP
  • DOMDocument
  • mb_convert_encoding
  • preg_match
Janitor
  • DOMElement->loadHTML
  • DOMElement->getElementsByTagName

DOM::stripAttributes

Definition

Name
stripAttributes
Syntax
Void = DOM::stripAttributes( String $node );

Description

Strip unwanted attributes from DOMElement and children.

Only the following attributes are allowed: href, class, width, height, alt, charset. The href must start with /, http://, https://, mailto:, tel:. Otherwise the href property will be removed.

Parameters

$node
DOMElement DOMElement to strip attributes from.

Return values

Void

Examples

DOM()->createDom('<span class="test" style="color: red;">hello world</span>'); DOM()->stripAttributes($dom);

DOM object $dom now has the following structure:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <meta> </head> <body> <span class="test">Hello world</span> </body> </html>

Dependencies

PHP
  • preg_match
  • foreach
  • if...else
Janitor
  • DOMElement->removeAttribute
  • DOMElement->getElementsByTagName