<html>
<head>
<title>HTMLPP - HyperText Markup Language Page Parser - Documentation and examples</title>
<link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/2.0.320/styles/shCore.css"/>
<link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/2.0.320/styles/shThemeDefault.css" id="shTheme"/>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shCore.js"></script>
<script type='text/javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushPhp.js'></script>
<script type='text/javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushJScript.js'></script>
<style>
body{
padding:0;
font-family:Arial,Verdana,sans-serif;
font-size:11px;
background: #CAD8F7;
}
h2{
margin:5px 0;
}
h3{
margin:20px 0 5px;
}
.page-wrapper{
height:100%;
}
.content-wrapper{
text-align:center;
vertical-align:top;
width:900px;
}
.nav{
height:40px;
-moz-border-radius-topleft:5px;
-webkit-border-top-left-radius:5px;
border-top-left-radius:5px;
-moz-border-radius-topright:5px;
-webkit-border-top-right-radius:5px;
border-top-right-radius:5px;
background:#D2E1F6;
}
.nav ul{
margin:0;
padding:0;
list-style:none;
}
.nav ul li{
float:left;
}
.nav ul li span{
cursor:pointer;
display:block;
width:146px;
margin-right:1px;
padding-top:10px;
height:40px;
text-align:center;
color:#166AEE;
}
.nav ul li:last-child span{
width:150px;
}
.nav ul li span.long{
padding-top:13px;
font-size:14px;
}
.nav ul li a.active span,.nav ul li a.hover span{
background:#166AEE;
-moz-border-radius-topleft:5px;
-webkit-border-top-left-radius:5px;
border-top-left-radius:5px;
-moz-border-radius-topright:5px;
-webkit-border-top-right-radius:5px;
border-top-right-radius:5px;
color:white;
}
.structure{
background:#FFF;
color:white;
text-align:left;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
-webkit-box-shadow: 5px 5px 10px #888;
-moz-box-shadow: 5px 5px 10px #888;
border:7px solid #FFF;
}
.content{
padding:15px 10px;
background:#166AEE;
-moz-border-radius-bottomleft:5px;
-webkit-border-bottom-left-radius:5px;
border-bottom-left-radius:5px;
-moz-border-radius-bottomright:5px;
-webkit-border-bottom-right-radius:5px;
border-bottom-right-radius:5px;
font-size:14px;
line-height:1.3;
font-family:Verdana,sans-serif;
}
.content a{
color:white;
text-decoration:underline;
}
.hiddenpanel{
display:none;
}
.header{
height:200px;
font-size:110px;
color:orange;
text-align:center;
text-shadow: #D2E1F6 2px 2px 2px;
}
.header .colordiff{
color:#166AEE;
}
.header .min-size{
font-size:33px;
display:block;
}
.version{
font-size:15px;
display:block;
color:#166AEE;
margin-top:9px;
}
.padd{
padding: 0 20px;
}
.arg{
/*border:1px solid white;*/
background: white;
color: #166AEE;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
margin:5px 0;
padding:5px;
font-size:12px;
}
.arg code,.return{
margin:0 20px;
}
</style>
<style>
.syntaxhighlighter{
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
background-color:#D2E1F6 !important;
border:1px solid white !important;
}
.syntaxhighlighter .line .number{
color:#166AEE!important;
}
.syntaxhighlighter .variable{
color:#0000DD!important;
}
.syntaxhighlighter .string, .syntaxhighlighter .string a {
color:#008000 !important;
}
.syntaxhighlighter .comments{
color:#FF8000!important;
}
.syntaxhighlighter .functions {
color:red !important;
}
</style>
<script type="text/javascript">
SyntaxHighlighter.all();
var tabOpened="introduction";
function hasClass(obj,cls)
{
return obj.className.indexOf(cls)>=0;
}
function addClass(obj,cls)
{
if(!hasClass(obj,cls)) obj.className+=" "+cls;
}
function removeClass(obj,cls)
{
obj.className=obj.className.replace(cls,"");
}
function changeTab(id,tab)
{
if(tabOpened==id) return;
addClass(document.getElementById(tabOpened),"hiddenpanel");
removeClass(document.getElementById(id),"hiddenpanel");
tabOpened=id;
var li=document.getElementById("navmenu").getElementsByTagName("li");
for(i=0;i<li.length;i++){
removeClass(li[i].getElementsByTagName("a")[0],"active");
}
addClass(tab,"active");
}
</script>
</head>
<body>
<table class="page-wrapper" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="content-wrapper" align="center">
<div class="structure">
<div class="header">
HTML<span class="colordiff">PP</span>
<span class="min-size">HyperText Markup Language <span class="colordiff">Page Parser</span></span>
<span class="version">Version 1.0.3</span>
</div>
<div class="nav">
<ul id="navmenu">
<li><a class="active" onClick="changeTab('introduction',this)" onMouseOver="addClass(this,'hover')" onMouseOut="removeClass(this,'hover')"><span>Introduction</span></a></li>
<li><a onClick="changeTab('htmlppclass',this)" onMouseOver="addClass(this,'hover')" onMouseOut="removeClass(this,'hover')"><span>HTMLPP</span></a></li>
<li><a onClick="changeTab('htmlnodeclass',this)" onMouseOver="addClass(this,'hover')" onMouseOut="removeClass(this,'hover')"><span>HTMLNode</span></a></li>
<li><a onClick="changeTab('htmlcollclass',this)" onMouseOver="addClass(this,'hover')" onMouseOut="removeClass(this,'hover')"><span class="long">HTMLCollection</span></a></li>
<li><a onClick="changeTab('iterator',this)" onMouseOver="addClass(this,'hover')" onMouseOut="removeClass(this,'hover')"><span class="long">HTMLFilterIterator</span></a></li>
<li><a onClick="changeTab('credits',this)" onMouseOver="addClass(this,'hover')" onMouseOut="removeClass(this,'hover')"><span>Credits</span></a></li>
</ul>
</div>
<div class="content" id="introduction">
<h2>Introduction</h2>
HTMLPP is a PHP library for HTML code parsing. It allows you to parse a HTML code string, build the relative DOM structure and work on it with methods similar to Javascript.<br>
Javascript sintax to create and append a div element to the next sibling of the first child of the body:
<pre class="brush: javascript; tab-size: 4;">
var elem=document.createElement("div");
document.body.childNodes[0].nextSibling.appendChild(elm);
</pre>
Same operation in PHP with HTMLPP:
<pre class="brush: php; tab-size: 4;">
$elem=&$document->createElement("div");
$document->body->childNodes[0]->nextSibling->appendChild(elm);
</pre>
<h2>Features</h2>
<ul>
<li>HTML parsing
<ul>
<li>Simple tags</li>
<li>Tags without closures</li>
<li>Autoclosing tags</li>
<li>Doctype, text and comment parsing</li>
<li>Modern browser parsing behaviour
<ul>
<li>Add head,body and html tags if they're not present</li>
<li>Wrap table content inside the tbody if it's not present</li>
</ul>
</li>
</ul>
</li>
<li>Dom traversing
<ul>
<li>Access to the parent node using the <code>parentNode</code> property</li>
<li>Access to child nodes using the <code>childNodes</code> array property</li>
<li>Access to sibling nodes using <code>nextSibling</code> and <code>previousSibling</code> properties</li>
<li>Access to the owner document with <code>ownerDocument</code> property</li>
<li>Document shortcuts to body, head and doctype</li>
</ul>
</li>
<li>Dom manipulation
<ul>
<li>Append nodes with <code>appendChild</code>, <code>append</code> and other methods</li>
<li>Remove nodes with <code>removeChild</code> and <code>remove</code> methods</li>
<li>Replace nodes with <code>replaceChild</code> and <code>replace</code> methods</li>
</ul>
</li>
<li>Attributes and style manipulation
<ul>
<li>Add, remove, set and get methods for attributes</li>
<li>Add, remove, set and get methods for style properties</li>
</ul>
</li>
<li>Node searching functions on every element
<ul>
<li><code>getElementById</code></li>
<li><code>getElementsByTagName</code></li>
<li><code>getElementsByClassName</code></li>
<li><code>getElementsBySelector</code>
<ul>
<li>Full featured support for Css3 selectors</li>
<li>Support for other non-standard selectors</li>
</ul>
</li>
<li>Node iterator class for personalized filter functions</li>
</ul>
</li>
<li>Dom collections with JQuery like methods
<ul>
<li>Add, remove and filter elements in the collection</li>
<li>Change the current collection by searching in its elements siblings, child nodes or parent nodes</li>
<li>Manipulate elements in the collection</li>
</ul>
</li>
</ul>
<h2>Usage</h2>
There are 4 steps to follow to use the class
<ol>
<li>Include the HTMLPP.php file</li>
<li>Create a new instance of the HTMLPP class</li>
<li>Load HTML code</li>
<li>Parse and get the document</li>
</ol>
<pre class="brush: php; tab-size: 4;">
require_once "HTMLPP.php"; /*include the file*/
$HTML=new HTMLPP; /*Create the instance*/
$HTML->loadHTMLFile("http://www.mysite.com"); /*Load the code*/
$document=& $HTML->getDocument(); /*Parse and get the document*/
</pre>
<h2>Changelog</h2>
<h3>1.0.3</h3>
<ul>
<li>Fixed bug in getAttribute() method</li>
<li>Fixed bug in getStyle() method</li>
<li>Fixed bug on attributes parsing</li>
</ul>
<h3>1.0.2</h3>
<ul>
<li>Fixed error on selector parsing</li>
<li>Now every element is closed at the end of its parent code if no closing tag is found</li>
<li>Better support for textarea tag</li>
<li>Fixed bug on attributes parsing (thanks Mike)</li>
</ul>
<h3>1.0.1</h3>
<ul>
<li>Fixed some bugs in elements parsing regexp</li>
<li>Fixed a bug in doctype parsing</li>
<li>Fixed some problems in the parser class</li>
<li>Fixed a bug in HTMLFilterIterator::find() function when pass HTML_SEARCH_DESCENDANT as iteration type</li>
</ul>
<h3>1.0</h3>
<ul>
<li>First release</li>
</ul>
</div>
<div class="content hiddenpanel" id="htmlppclass">
Main HTMLPP class
<h2>Public properties</h2>
<div class="padd">
<h3>code</h3>
<div class="padd"><span class="return">String</span>The code to be parsed. This variable is set by the loading functions.</div>
<h3>document</h3>
<div class="padd"><span class="return">Object</span>Reference to the parsed document. This is created by calling the getDocument function.</div>
</div>
<h2>Public methods</h2>
<div class="padd">
<h3>loadHTML</h3>
<div class="padd">
Load HTML code by string.
<br><b>Arguments</b>
<div class="arg">
String <code>$string</code> HTML code string
</div>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$HTML=new HTMLPP;
$HTML->loadHTML("<div>Example</div>");
</pre>
</div>
<h3>loadHTMLFile</h3>
<div class="padd">
Get and load HTML code from a local file or a URL.
<br><b>Arguments</b>
<div class="arg">
String <code>$path</code> HTML file path<br>
Bool <code>$useCurl</code> If it's true(default) and the function cannot load the contents directly it uses cUrl functions<br>
Array <code>$curlOpt</code> An array (format: option=>value) for curl settings
</div>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$HTML=new HTMLPP;
$HTML->loadHTMLFile("http://www.mysite.com");
</pre>
</div>
<h3>stripComments</h3>
<div class="padd">Strip all comments in the code. This function must be called before call the getDocument function.
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$HTML=new HTMLPP;
$HTML->loadHTML("<div>Example</div><!--comment-->");
$HTML->stripComments(); //Now all comments are removed
$document=& $HTML->getDocument();
</pre>
</div>
<h3>getDocument</h3>
<div class="padd">Return a reference to the parsed document.
<br><b>Return</b><br><span class="return">Object</span> Reference to the parsed document
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$HTML=new HTMLPP;
$HTML->loadHTMLFile("http://www.mysite.com");
$document=& $HTML->getDocument();
</pre>
</div>
<h3>render</h3>
<div class="padd">Return document HTML code as a string.
<br><b>Return</b><br><span class="return">String</span> Document HTML code
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$HTML=new HTMLPP;
$HTML->loadHTMLFile("http://www.mysite.com");
$document=& $HTML->getDocument();
echo $HTML->render();
</pre>
</div>
</div>
</div>
<div class="content hiddenpanel" id="htmlnodeclass">
HTML elements, comments, text, the doctype and the document are instances of this class. It provides methods to work on the DOM tree and manipulate elements. <br>The document has some additional methods to create elements.
<h2>Predefined constants</h2>
<div class="padd">
<h3>Node type constants</h3>
<div class="padd">
<span class="return"><b>ELEMENT_NODE</b></span>HTML element node type<br>
<span class="return"><b>TEXT_NODE</b></span>Text node type<br>
<span class="return"><b>COMMENT_NODE</b></span>Comment node type<br>
<span class="return"><b>DOCUMENT_NODE</b></span>Document node type<br>
<span class="return"><b>DOCUMENT_TYPE_NODE</b></span>Doctype node type
</div>
</div>
<h2>Public properties</h2>
<div class="padd">
<h3>childNodes</h3>
<div class="padd"><span class="return">Array</span>Array that contains every child node of the element</div>
<h3>parentNode</h3>
<div class="padd"><span class="return">Object</span>Reference to the parent node of the element or null if there isn't</div>
<h3>previousSibling</h3>
<div class="padd"><span class="return">Object</span>Reference to the previous sibling of the element or null if there isn't</div>
<h3>nextSibling</h3>
<div class="padd"><span class="return">Object</span>Reference to the next sibling of the element or null if there isn't</div>
<h3>firstChild</h3>
<div class="padd"><span class="return">Object</span>Reference to the first child node of the element or null if there isn't</div>
<h3>lastChild</h3>
<div class="padd"><span class="return">Object</span>Reference to the last child node of the element or null if there isn't</div>
<h3>ownerDocument</h3>
<div class="padd"><span class="return">Object</span>Reference to the document object</div>
<h3>attributes</h3>
<div class="padd"><span class="return">Object</span>Object that contains element's attributes</div>
<h3>style</h3>
<div class="padd"><span class="return">Object</span>Object that contains element's style properties</div>
<h3>index</h3>
<div class="padd"><span class="return">Int</span>Element's index in its parent node childNodes array</div>
<h3>nodeType</h3>
<div class="padd"><span class="return">Int</span>Element's node type. It's value is one of the node type constants</div>
<h3>nodeName</h3>
<div class="padd"><span class="return">String</span>Element's node name</div>
<h3>nodeValue</h3>
<div class="padd"><span class="return">String</span>Element's node value</div>
<h3>tagName</h3>
<div class="padd"><span class="return">String</span>Element's tag name. Null for document, doctype, comments and text nodes</div>
<h3>textContent</h3>
<div class="padd"><span class="return">String</span>It contains the text of text and comments nodes and the code inside script and style tags</div>
<h3>name</h3>
<div class="padd"><span class="return">String</span>This property is owned only by the doctype element and it's the doctype name</div>
<h3>publicId</h3>
<div class="padd"><span class="return">String</span>This property is owned only by the doctype element and it's the doctype public id</div>
<h3>systemId</h3>
<div class="padd"><span class="return">String</span>This property is owned only by the doctype element and it's the doctype system id</div>
</div>
<h2>Document special methods</h2>
<div class="padd">
<h3>createElement</h3>
<div class="padd">
Create a new element.
<br><b>Arguments</b>
<div class="arg">
String <code>$tag</code> Element's tag name
</div>
<b>Return</b><br><span class="return">Object</span> Created element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->createElement('div'); //Create a new div element
</pre>
</div>
<h3>createTextNode</h3>
<div class="padd">
Create a new text node.
<br><b>Arguments</b>
<div class="arg">
String <code>$text</code> Text inside the node
</div>
<b>Return</b><br><span class="return">Object</span> Created text node<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->createTextNode('example text');
</pre>
</div>
<h3>createComment</h3>
<div class="padd">
Create a new comment node.
<br><b>Arguments</b>
<div class="arg">
String <code>$text</code> Comment text
</div>
<b>Return</b><br><span class="return">Object</span> Created comment node<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->createComment('comment text');
</pre>
</div>
<h3>createDocumentType</h3>
<div class="padd">
Create a new doctype node.
<br><b>Arguments</b>
<div class="arg">
String <code>$name</code> Name<br>
String <code>$publicId</code> Public id<br>
String <code>$systemId</code> System id
</div>
<b>Return</b><br><span class="return">Object</span> Created doctype node<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->createDocumentType('html','-//W3C//DTD XHTML 1.0 Strict//EN','http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
</pre>
</div>
</div>
<h2>Public methods</h2>
<div class="padd">
<h3>setAttributes</h3>
<div class="padd">
Set multiple attributes
<br><b>Arguments</b>
<div class="arg">
mixed <code>$attributes</code> Array or object of attributes in attribute=>value format or html attribute string
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->body->childNodes[2];
$element->setAttributes(array("class"=>"myclass","id"=>"myid")); //array
$element->setAttributes("class='myclass' id='myid'"); //html attribute string
</pre>
</div>
<h3>setAttribute</h3>
<div class="padd">
Set an attribute
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Attribute name<br>
string <code>$val</code> Attribute value
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->body->firstChild;
$element->setAttribute("name","myname");
</pre>
</div>
<h3>hasAttribute</h3>
<div class="padd">
Check if the element has a given attribute
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Attribute name
</div>
<b>Return</b><br><span class="return">Bool</span> True if the element has that attribute, otherwise false<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
if($element->parentNode->hasAttribute("id"))
{
...
}
</pre>
</div>
<h3>getAttribute</h3>
<div class="padd">
Return the value of the given attribute or null if the element hasn't that attribute
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Attribute name
</div>
<b>Return</b><br><span class="return">Mixed</span> The attribute value or null if the element hasn't that attribute<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$id=$element->getAttribute("id");
</pre>
</div>
<h3>removeAttribute</h3>
<div class="padd">
Remove the given attribute from the element
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Attribute name
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element->setAttribute("id","myid");
$element->removeAttribute("id");
$id=$element->getAttribute("id"); //null because it was removed
</pre>
</div>
<h3>hasAttributes</h3>
<div class="padd">
Check if the element has at least one attribute
<br>
<b>Return</b><br><span class="return">Bool</span> True if the element has at least one attribute, otherwise false<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=&$document->createElement("span");
$check=$element->hasAttributes(); //False
$element->setAttribute("id","myid");
$check=$element->hasAttributes(); //True
</pre>
</div>
<h3>emptyAttributes</h3>
<div class="padd">
Remove all attributes from the element
<br>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$check=$element->hasAttributes(); //True
$element->emptyAttributes();
$check=$element->hasAttributes(); //False
</pre>
</div>
<h3>setStyles</h3>
<div class="padd">
Set multiple style properties
<br><b>Arguments</b>
<div class="arg">
mixed <code>$styles</code> Array or object of style properties in style=>value format or css style string
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->body->childNodes[2];
$element->setStyles(array("padding"=>"5px","z-index"=>"2")); //array
$element->setStyles("padding:5px; z-index:2"); //css style string
</pre>
</div>
<h3>setStyle</h3>
<div class="padd">
Set a style property
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Style property name<br>
string <code>$val</code> Style property value
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->body->firstChild;
$element->setStyle("margin","1px");
</pre>
</div>
<h3>hasStyle</h3>
<div class="padd">
Check if the element has a given style property
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Style property name
</div>
<b>Return</b><br><span class="return">Bool</span> True if the element has that style property, otherwise false<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
if($element->parentNode->hasStyle("background"))
{
...
}
</pre>
</div>
<h3>getStyle</h3>
<div class="padd">
Return the value of the given style property or null if the element hasn't that style property
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Style property name
</div>
<b>Return</b><br><span class="return">Mixed</span> The style property value or null if the element hasn't that style property<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$color=$element->getStyle("color");
</pre>
</div>
<h3>removeStyle</h3>
<div class="padd">
Remove the given style property from the element
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Style property name
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element->setStyle("font-size","12px");
$element->removeStyle("font-size");
$size=$element->getStyle("font-size"); //null because it was removed
</pre>
</div>
<h3>hasStyles</h3>
<div class="padd">
Check if the element has at least one style property
<br>
<b>Return</b><br><span class="return">Bool</span> True if the element has at least one style property, otherwise false<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=&$document->createElement("span");
$check=$element->hasStyles(); //False
$element->setStyle("text-decoration","underline");
$check=$element->hasStyles(); //True
</pre>
</div>
<h3>emptyStyles</h3>
<div class="padd">
Remove all style properties from the element
<br>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$check=$element->hasStyles(); //True
$element->emptyStyles();
$check=$element->hasStyles(); //False
</pre>
</div>
<h3>setCssText</h3>
<div class="padd">
Same as setStyles, but it empties the style object before setting new style properties
<br><b>Arguments</b>
<div class="arg">
string <code>$style</code> Style properties in css formatted string
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element->setCssText("background:red;margin:0");
</pre>
</div>
<h3>getCssText</h3>
<div class="padd">
Get the style object in css formatted string
<br>
<b>Return</b><br><span class="return">String</span> Css formatted string of the style object<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=&$document->createElement("table");
$element->setCssText("background:red; margin:0");
echo $element->getCssText(); //returns "background:red;margin:0"
</pre>
</div>
<h3>setInnerHTML</h3>
<div class="padd">
Set element's inner code by parsing the given HTML string
<br><b>Arguments</b>
<div class="arg">
string <code>$HTML</code> HTML code
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->head->firstChild->nextSibling;
echo count($element->childNodes); //0: this element hasn't got any child node
$element->setInnerHTML("<title>Example</title>");
echo count($element->childNodes); //1: Now the element has a child node
echo $element->childNodes[0]->tagName; //1: The child node is a title element
</pre>
</div>
<h3>getInnerHTML</h3>
<div class="padd">
Get the HTML code inside the element.
<br>
<b>Return</b><br><span class="return">String</span> HTML code inside the element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element->setInnerHTML("<title>Example</title>");
echo $element->getInnerHTML(); //Prints "<title>Example</title>"
</pre>
</div>
<h3>getOuterHTML</h3>
<div class="padd">
Get the HTML code of the element and its innerHTML.
<br>
<b>Return</b><br><span class="return">String</span> HTML element code and the code inside it<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->createElement("span");
$element->setInnerHTML("<div>Example</div>");
echo $element->getOuterHTML(); //Prints "<span><div>Example</div></span>"
</pre>
</div>
<h3>getTextContent</h3>
<div class="padd">
Return the text content of the element and its children
<br>
<b>Return</b><br><span class="return">String</span> Text content<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->createElement("span");
$element->setInnerHTML("This is an <div>Example</div>");
echo $element->getTextContent(); //Prints "This is an Example"
</pre>
</div>
<h3>getOuterTextContent</h3>
<div class="padd">
Return the getTextContent function applied on the current element and its sibling nodes
<br>
<b>Return</b><br><span class="return">String</span> Text content<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->createElement("span");
$element->setInnerHTML("<span>This is an </span><div>Example</div>");
echo $element->childNodes[0]->getOuterTextContent(); //Prints "This is an Example"
</pre>
</div>
<h3>getChildAt</h3>
<div class="padd">
Get the child node at the specified position or null if there's no element at that index
<br><b>Arguments</b>
<div class="arg">
int <code>$index</code> Child node index
</div>
<b>Return</b><br><span class="return">Object</span> HTML element or null if there's no element at that index<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$child=& $element->getChildAt(2);
$child=& $element->->childNodes[2]; //Same operation
</pre>
</div>
<h3>getElementsByTagName</h3>
<div class="padd">
Get elements inside the current node with the given tag name. It returns an HTMLCollection object.
<br><b>Arguments</b>
<div class="arg">
string <code>$tag</code> Tag name
</div>
<b>Return</b><br><span class="return">Object</span> HTML collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$allDivs=& $document->body->getElementsByTagName("div"); //Find all div elements inside the body
</pre>
</div>
<h3>getElementsByClassName</h3>
<div class="padd">
Get elements inside the current node with the given class name. It returns an HTMLCollection object.
<br><b>Arguments</b>
<div class="arg">
string <code>$class</code> Class name
</div>
<b>Return</b><br><span class="return">Object</span> HTML collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$class=& $document->body->getElementsByClassName("myclass"); //Find all elements with the given class attribute
</pre>
</div>
<h3>getElementById</h3>
<div class="padd">
Get the element with the given id inside the current node or null if there's no element with that id
<br><b>Arguments</b>
<div class="arg">
string <code>$id</code> Id
</div>
<b>Return</b><br><span class="return">Object</span> HTML element or null<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$myid=& $document->body->getElementsById("myid"); //Find the element with "myid" id
</pre>
</div>
<h3>getElementsBySelector</h3>
<div class="padd">
Get every element inside the current element that matches the given css selector. For a list of all supported selectors see the HTMLCollection section.
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code> Css selector
</div>
<b>Return</b><br><span class="return">Object</span> HTML collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("div.myclass"); //Find all divs with "myclass" class name
</pre>
</div>
<h3>getElementsBySelector</h3>
<div class="padd">
Get every element inside the current element that matches the given css selector. For a list of all supported selectors see the HTMLCollection section.
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code> Css selector
</div>
<b>Return</b><br><span class="return">Object</span> HTML collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("div.myclass"); //Find all divs with "myclass" class name
</pre>
</div>
<h3>isSameNode</h3>
<div class="padd">
Return true if the given element and the current are the same node.
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> Element for the comparison
</div>
<b>Return</b><br><span class="return">Bool</span> True if the given element and the current are the same node, otherwise false<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$el1=& $document->getChildAt(0);
$el2=& $document->childNodes[0];
$check= $el1->isSameNode($el2); //true
</pre>
</div>
<h3>cloneNode</h3>
<div class="padd">
Return a copy of the node
<br><b>Arguments</b>
<div class="arg">
bool <code>$deep</code> If it's set to true child nodes are cloned too, otherwise(default) only the element is cloned
</div>
<b>Return</b><br><span class="return">Object</span> Copy of the node<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$copy= & $element->cloneNode();
</pre>
</div>
<h3>hasChildNodes</h3>
<div class="padd">
Return true if the element has at least one child node
<br>
<b>Return</b><br><span class="return">Bool</span> True if the element has at least one child node, otherwise false<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element->hasChildNodes(); //false
$element->setInnerHTML("<div></div>");
$element->hasChildNodes(); //true
</pre>
</div>
<h3>appendChild</h3>
<div class="padd">
Insert the given element at the end of the current element's child nodes array
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> Element to insert
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->getElementById("element");
$document->body->appendChild($element); //Append the element to the body
</pre>
</div>
<h3>appendTo</h3>
<div class="padd">
Insert the current element at the end of the given element's child nodes array
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> Element in which to insert the current element
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->getElementById("element");
$element->appendTo($document->body); //Append the element to the body
</pre>
</div>
<h3>prependChild</h3>
<div class="padd">
Insert the given element at the beginning of the current element's child nodes array
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> Element to insert
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->getElementById("element");
$document->body->prependChild($element); //Prepend the element to the body
</pre>
</div>
<h3>prependTo</h3>
<div class="padd">
Insert the current element at the beginning of the given element's child nodes array
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> Element in which to insert the current element
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->getElementById("element");
$element->prependTo($document->body); //Prepend the element to the body
</pre>
</div>
<h3>insertChildAt</h3>
<div class="padd">
Insert the given element at the specified position of the current element's child nodes array
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> Element to insert<br>
int <code>$index</code> Zero based position index
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->getElementById("element");
$document->body->insertChildAt($element,1); //Insert the element in second position
</pre>
</div>
<h3>insertAt</h3>
<div class="padd">
Insert the current element at the specified position of the given element's child nodes array
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> Element in which to insert the current element<br>
int <code>$index</code> Zero based position index
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->getElementById("element");
$element->insertAt($document->body,2); //Insert the element in second position
</pre>
</div>
<h3>insertBefore</h3>
<div class="padd">
Insert the second element before the first in the current element child nodes collection
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> Before this element the second one will be inserted<br>
object <code>$node</code> Element to insert
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$document->body->insertBefore($element,$node); //Insert the element before the second child of the body
</pre>
</div>
<h3>insertBeforeNode</h3>
<div class="padd">
Insert the current element before the given one
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> Before this element the current one will be inserted
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$node->insertBeforeNode($element); //Insert the element before the second child of the body
</pre>
</div>
<h3>insertAfter</h3>
<div class="padd">
Insert the second element after the first in the current element child nodes collection
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> After this element the second one will be inserted<br>
object <code>$node</code> Element to insert
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$document->body->insertAfter($element,$node); //Insert the element after the second child of the body
</pre>
</div>
<h3>insertAfterNode</h3>
<div class="padd">
Insert the current element after the given one
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> After this element the current one will be inserted
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$node->insertAfterNode($element); //Insert the element after the second child of the body
</pre>
</div>
<h3>replaceChild</h3>
<div class="padd">
Replace the first element with the second one in the current element child nodes array
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> The element that must be replaced<br>
object <code>$replacement</code> The element used to replace the first one
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$node->replaceChild($element,$node); //Replace the first child of the body with the element
</pre>
</div>
<h3>replace</h3>
<div class="padd">
Replace the given element with the current one
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> The element that must be replaced
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$node=& $document->getElementById("element");
$element=& $document->body->childNodes[1];
$node->replace($element); //Replace the first child of the body with the element
</pre>
</div>
<h3>removeChild</h3>
<div class="padd">
Remove the given element from the current element child nodes array
<br><b>Arguments</b>
<div class="arg">
object <code>$element</code> The element that must be removed
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->body->childNodes[0];
$document->body->removeChild($element); //Remove the first child of the body
</pre>
</div>
<h3>removeChildAt</h3>
<div class="padd">
Remove element at the given index from the current element child nodes array
<br><b>Arguments</b>
<div class="arg">
int <code>$index</code> The zero based index of the element that must be removed
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$document->body->removeChild(0); //Remove the first child of the body
</pre>
</div>
<h3>removeChildAt</h3>
<div class="padd">
Remove the current element from its parent node child nodes array
<br>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$document->body->childNodes[0]->remove(); //Remove the first child of the body
</pre>
</div>
<h3>emptyChildNodes</h3>
<div class="padd">
Remove every child node from the current element
<br>
<b>Return</b><br><span class="return">Object</span> Reference to the current element<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$document->body->hasChildNodes(); //True
$document->body->emptyChildNodes(); //Remove the first child of the body
$document->body->hasChildNodes(); //False
</pre>
</div>
</div>
</div>
<div class="content hiddenpanel" id="htmlcollclass">
HTML collection class. This class provides methods to select and manipulate a group of elements by searching them with css3 selectors queries.
<h2>Supported selectors</h2>
<div class="padd">
<h3>Tag name, class, id</h3>
<div class="padd">
<span class="return"><b>tag</b></span>Get elements by tag name.<br>
<span class="return"><b>#id</b></span>Get elements by id<br>
<span class="return"><b>.class</b></span>Get elements by class name.<br>
</div>
<h3>Attributes</h3>
<div class="padd">
<span class="return"><b>[attribute]</b></span>Get elements that have the given attribute<br>
<span class="return"><b>[attribute=value]</b></span>Get elements with the given attribute equal to the given value<br>
<span class="return"><b>[attribute!=value]</b></span>Get elements with the given attribute different from the given value<br>
<span class="return"><b>[attribute$=value]</b></span>Get elements with the given attribute that ends with the given value<br>
<span class="return"><b>[attribute|=value]</b></span>Get elements with the given attribute that is equal to the given value or is equal to the given value with a hypen before and/or after<br>
<span class="return"><b>[attribute^=value]</b></span>Get elements with the given attribute that starts with the given value<br>
<span class="return"><b>[attribute~=value]</b></span>Get elements with the given attribute that is equal to the given value or is equal to the given value with a whitespace before and/or after<br>
<span class="return"><b>[attribute*=value]</b></span>Get elements with the given attribute that contains the given value<br>
</div>
<h3>Pseudo selectors</h3>
<div class="padd">
<span class="return"><b>:first-child</b></span>Get elements that are first child of their parent<br>
<span class="return"><b>:last-child</b></span>Get elements that are last child of their parent<br>
<span class="return"><b>:only-child</b></span>Get elements that are the only child of their parent<br>
<span class="return"><b>:nth-child(exp)</b></span>Get elements that have an+b-1 siblings before them in the document tree<br>
<span class="return"><b>:nth-last-child(exp)</b></span>Get elements that have an+b-1 siblings after them in the document tree<br>
<span class="return"><b>:odd</b></span>Get elements in odd position<br>
<span class="return"><b>:even</b></span>Get elements in even position<br>
<span class="return"><b>:first-of-type</b></span>Get elements that are first child of their type in their parent's children<br>
<span class="return"><b>:last-of-type</b></span>Get elements that are lst child of their type in their parent's children<br>
<span class="return"><b>:only-of-type</b></span>Get elements that are the only child of their type in their parent's children<br>
<span class="return"><b>:nth-of-type</b></span>Get elements that have an+b-1 siblings of their type before them in the document tree<br>
<span class="return"><b>:nth-last-of-type(exp)</b></span>Get elements that have an+b-1 siblings of their type after them in the document tree<br>
<span class="return"><b>:contains(text)</b></span>Get elements that contain the given text<br>
<span class="return"><b>:has(selector)</b></span>Get elements that contain at least one element that matches the given selector<br>
<span class="return"><b>:not(selector)</b></span>Get elements that don't match the given selector<br>
<span class="return"><b>:empty</b></span>Get elements that don't contain any element or text node<br>
<span class="return"><b>:parent</b></span>Get elements that are parent of at least one element or text node<br>
<span class="return"><b>:header</b></span>Get H1,H2,H3,H4,H5,H6 elements<br>
<span class="return"><b>:button</b></span>Get button elements or input elements with type=button<br>
<span class="return"><b>:input</b></span>Get input, button, select and textarea elements<br>
<span class="return"><b>:text</b></span>Get input elements with type=text<br>
<span class="return"><b>:reset</b></span>Get input elements with type=reset<br>
<span class="return"><b>:file</b></span>Get input elements with type=file<br>
<span class="return"><b>:radio</b></span>Get input elements with type=radio<br>
<span class="return"><b>:password</b></span>Get input elements with type=password<br>
<span class="return"><b>:submit</b></span>Get input elements with type=submit<br>
<span class="return"><b>:image</b></span>Get input elements with type=image<br>
<span class="return"><b>:hidden</b></span>Get input elements with type=hidden<br>
<span class="return"><b>:checkbox</b></span>Get input elements with type=checkbox<br>
<span class="return"><b>:selected</b></span>Get selected elements<br>
<span class="return"><b>:checked</b></span>Get checked elements<br>
<span class="return"><b>:disabled</b></span>Get disabled elements<br>
<span class="return"><b>:readonly</b></span>Get read-only elements<br>
<span class="return"><b>:enabled</b></span>Get enabled elements<br>
<span class="return"><b>:lang(value)</b></span>Get elements with the lang attribute equal to the given value<br>
<span class="return"><b>:root</b></span>Get HTML element<br>
</div>
<h3>Combinators</h3>
<div class="padd">
<span class="return"><b>" "(whitespace)</b></span>Search in elements subtrees<br>
<span class="return"><b>">"</b></span>Search in elements child nodes<br>
<span class="return"><b>"+"</b></span>Search only in elements next sibling<br>
<span class="return"><b>"~"</b></span>Search in all elements next sibling<br>
</div>
For a better explanation on css3 selectors visit: <a href="http://www.w3.org/TR/css3-selectors/" target="_blank">http://www.w3.org/TR/css3-selectors/</a>
<h2>Public properties</h2>
<div class="padd">
<h3>elements</h3>
<div class="padd"><span class="return">Array</span>Array that contains every element of the collection</div>
<h3>length</h3>
<div class="padd"><span class="return">Int</span>Number of elements in the collection</div>
<h3>context</h3>
<div class="padd"><span class="return">Object</span>Original node from witch the collection was generated</div>
</div>
</div>
<h2>Constructor</h2>
<div class="padd">
<h3>HTMLCollection</h3>
<div class="padd">
Class constructor
<br><b>Arguments</b>
<div class="arg">
object <code>$context</code>The node from which to start searching<br>
string <code>$selector</code>An optional selector for immediatly fill the collection<br>
</div>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$element=& $document->body;
$coll=new HTMLCollection($element,"div"); //Contains every div element inside the body
$coll=new HTMLCollection($element); //Empty collection
</pre>
</div>
</div>
<h2>Public methods</h2>
<div class="padd">
<h3>merge</h3>
<div class="padd">
Merge the current collection with another element, array of element or another collection
<br><b>Arguments</b>
<div class="arg">
mixed <code>$element</code>Element, array of elements, collection, array of collections
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsByTagName("span");
$col->merge($document->body); //Add the body element to the collection
$divs=& $document->getElementsByTagName("div");
$col->merge($divs); //Merge with another collection
</pre>
</div>
<h3>add</h3>
<div class="padd">
Add another set of elements that match the selector to the current collection
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsByClassName("class");
$col->add("div#myid"); //Add all div elements with "myid" id to the collection
</pre>
</div>
<h3>unique</h3>
<div class="padd">
Remove dupliacated elements from the collection.
<br>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("div span");
$col->unique(); //Now there are no duplicated elements
</pre>
</div>
<h3>each</h3>
<div class="padd">
Apply a function to all elements in the collection
<br><b>Arguments</b>
<div class="arg">
function <code>$fn</code>Function returned by create_function. It receives two parameters: the element passed as reference and it's index in the collection<br>
array<code>$args</code>Array of additional arguments to pass into the function
</div>
<b>Return</b><br><span class="return">Array</span> Array of every result returned by each function execution<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("p a:first-child");
$function=create_function(
'$element,$index,$name',
'$element->setAttribute("$name",$index);'
);
$args=array("rel");
$col->each($function,$args);
</pre>
</div>
<h3>setAttributes</h3>
<div class="padd">
Set multiple attributes for each element in the collection
<br><b>Arguments</b>
<div class="arg">
mixed <code>$attributes</code> Array or object of attributes in attribute=>value format or html attribute string
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("table");
$col->setAttributes(array("class"=>"myclass","id"=>"myid")); //array
$col->setAttributes("class='myclass' id='myid'"); //html attribute string
</pre>
</div>
<h3>setAttribute</h3>
<div class="padd">
Set an attribute for each element in the collection
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Attribute name<br>
string <code>$val</code> Attribute value
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("table");
$col->setAttribute("name","myname");
</pre>
</div>
<h3>getAttribute</h3>
<div class="padd">
Return an array of values of the given attribute for each element in the collection
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Attribute name
</div>
<b>Return</b><br><span class="return">Array</span>Array of attribute values<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("table");
$array=$col->getAttribute("name");
</pre>
</div>
<h3>removeAttribute</h3>
<div class="padd">
Remove the given attribute from each element in the collection
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Attribute name
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("table");
$col->removeAttribute("id");
</pre>
</div>
<h3>emptyAttributes</h3>
<div class="padd">
Remove all attributes from each element
<br>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("table");
$col->emptyAttributes();
</pre>
</div>
<h3>setStyles</h3>
<div class="padd">
Set multiple style properties for each element in the collection
<br><b>Arguments</b>
<div class="arg">
mixed <code>$styles</code> Array or object of style properties in style=>value format or css style string
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("span");
$col->setStyles(array("padding"=>"5px","z-index"=>"2")); //array
$col->setStyles("padding:5px; z-index:2"); //css style string
</pre>
</div>
<h3>setStyle</h3>
<div class="padd">
Set a style property for each element in the collection
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Style property name<br>
string <code>$val</code> Style property value
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("span");
$col->setStyle("margin","1px");
</pre>
</div>
<h3>getStyle</h3>
<div class="padd">
Return an array of values of the given style property for each element in the collection
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Style property name
</div>
<b>Return</b><br><span class="return">Array</span>Array of style properties<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("span");
$colors=$col->getStyle("color");
</pre>
</div>
<h3>removeStyle</h3>
<div class="padd">
Remove the given style property from each element in the collection
<br><b>Arguments</b>
<div class="arg">
string <code>$name</code> Style property name
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("span");
$col->removeStyle("color");
</pre>
</div>
<h3>emptyStyles</h3>
<div class="padd">
Remove all style properties from each element in the collection
<br>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("span");
$col->emptyStyles();
</pre>
</div>
<h3>andSelf</h3>
<div class="padd">
Add the context of the collection at the beginning of the collection's elements array
<br>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("span");
$col->andSelf(); //Now the collection contains the body too
</pre>
</div>
<h3>get</h3>
<div class="padd">
Return the element at the specified index or null if there isn't any element at that index
<br><b>Arguments</b>
<div class="arg">
int <code>$index</code> Zero based position index
</div>
<b>Return</b><br><span class="return">Mixed</span> HTML elment at the specified position on null<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("span");
$span=& $col->get(1); //Get the second element
</pre>
</div>
<h3>copy</h3>
<div class="padd">
Returns a copy of the current collection
<br>
<b>Return</b><br><span class="return">Object</span> Copy of the collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("span");
$copy=& $col->copy();
</pre>
</div>
<h3>eq</h3>
<div class="padd">
Reduce the collection to only one element that is positioned at the given index
<br><b>Arguments</b>
<div class="arg">
int <code>$index</code> Zero based position index
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->body->getElementsByTagName("span");
echo $col->length; //10
$col->eq(0);
echo $col->length; //1
</pre>
</div>
<h3>slice</h3>
<div class="padd">
Reduce the collection by setting a range of elements positions. Elements at the start and end positions are included.
<br><b>Arguments</b>
<div class="arg">
int <code>$start</code> Start position. If it's a negative number it indecates the number of elements that must be taken before the position indicated by the second parameter<br>
int <code>$end</code> End position. If it's not set the last element's position will be taken
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col->slice(2); //Remove only the first two elements
$col->slice(2,4); //Get only elements in 2,3 and 4 positions
$col->slice(-2); //Get last two elements
</pre>
</div>
<h3>filterByFunction</h3>
<div class="padd">
Filter the elements in the collection by passing them to a given function. If this function returns false the relative element is removed.
<br><b>Arguments</b>
<div class="arg">
function <code>$fn</code>Function returned by create_function. It receives two parameters: the element passed as reference and it's index in the collection<br>
array<code>$args</code>Array of additional arguments to pass into the function
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsByTagName("p");
$function=create_function(
'$element,$index,$name',
'return $element->getAttribute("name")==$name;'
);
$args=array("test");
$col->each($function,$args); //Get all p elements with name=test
</pre>
</div>
<h3>filter</h3>
<div class="padd">
Remove every element in the collection that doesn't match the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsByTagName("p");
$col->filter(".myclass"); //Get only p elements with class=myclass
</pre>
</div>
<h3>is</h3>
<div class="padd">
Return true if at least one element matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Bool</span> True if at least one element matches the given selector, otherwise false<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsByTagName("p");
if($col->is("[name=myname]")) //True if at least one element has name=myname
{
...
}
</pre>
</div>
<h3>all</h3>
<div class="padd">
Return true if every element in the collection matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Bool</span> True if every element in the collection matches the given selector, otherwise false<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsByTagName("p");
if($col->all("[name=myname]")) //True if all p has name=myname
{
...
}
</pre>
</div>
<h3>remove</h3>
<div class="padd">
Remove from the collection every element that matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsByTagName("p");
$col->remove(".myclass"); //Remove every p with class=myclass
</pre>
</div>
<h3>children</h3>
<div class="padd">
Replace the current collection with every direct child of each element in the collection that matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("a");
$col->children("span.myclass"); //Now the collection contains every span element with class=myclass that is child of an a element matched by the first operation
</pre>
</div>
<h3>find</h3>
<div class="padd">
Replace the current collection with every descendant child of each element in the collection that matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("a");
$col->find("span.myclass"); //Now the collection contains every span element with class=myclass that is contained by an a element matched by the first operation
</pre>
</div>
<h3>next</h3>
<div class="padd">
Replace the current collection with every first next sibling of each element in the collection that matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("a");
$col->next("span"); //Now the collection contains every span element that is the nextSibling of an a element matched by the first operation
</pre>
</div>
<h3>nextAll</h3>
<div class="padd">
Replace the current collection with every next sibling of each element in the collection that matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("a");
$col->nextAll("span"); //Now the collection contains every span element that is one of the next siblings of an a element matched by the first operation
</pre>
</div>
<h3>prev</h3>
<div class="padd">
Replace the current collection with every first previous sibling of each element in the collection that matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("a");
$col->prev("span"); //Now the collection contains every span element that is the previousSibling of an a element matched by the first operation
</pre>
</div>
<h3>prevAll</h3>
<div class="padd">
Replace the current collection with every previous sibling of each element in the collection that matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("a");
$col->prevAll("span"); //Now the collection contains every span element that is one of the previous siblings of an a element matched by the first operation
</pre>
</div>
<h3>siblings</h3>
<div class="padd">
Replace the current collection with every sibling of each element in the collection that matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("a");
$col->siblings("span"); //Now the collection contains every span element that is one of the siblings of an a element matched by the first operation
</pre>
</div>
<h3>parent</h3>
<div class="padd">
Replace the current collection with the parent node of each element in the collection that matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("a");
$col->parent("span"); //Now the collection contains every span element that is the parentNode of an a element matched by the first operation
</pre>
</div>
<h3>parents</h3>
<div class="padd">
Replace the current collection with every ancestor node of each element in the collection that matches the given selector
<br><b>Arguments</b>
<div class="arg">
string <code>$selector</code>Selector
</div>
<b>Return</b><br><span class="return">Object</span> Reference to the current collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$col=& $document->getElementsBySelector("a");
$col->parents("span"); //Now the collection contains every span element that contains an a element matched by the first operation
</pre>
</div>
</div>
</div>
<div class="content hiddenpanel" id="iterator">
HTML filter iterator class. This class provides a way to build a DOMCollection executing a given function, choosing an iteration type and selecting which nodes will be included in the search.
<h2>Predefined constants</h2>
<div class="padd">
<h3>Iteration type constants</h3>
<div class="padd">
<span class="return"><b>HTML_SEARCH_DESCENDANT</b></span>earch in element's descendant nodes<br>
<span class="return"><b>HTML_SEARCH_CHILDREN</b></span>Search in element's children<br>
<span class="return"><b>HTML_SEARCH_NEXT_SIBLING</b></span>Search only element's next sibling<br>
<span class="return"><b>HTML_SEARCH_ALL_NEXT_SIBLINGS</b></span>Search in element's next siblings<br>
<span class="return"><b>HTML_SEARCH_PREVIOUS_SIBLING</b></span>Search only element's previous sibling<br>
<span class="return"><b>HTML_SEARCH_ALL_PREVIOUS_SIBLINGS</b></span>Search in element's previous siblings<br>
<span class="return"><b>HTML_SEARCH_SIBLINGS</b></span>Search in all element's siblings (next and previous)<br>
<span class="return"><b>HTML_SEARCH_PARENT</b></span>Search in element's parent node<br>
<span class="return"><b>HTML_SEARCH_ANCESTOR</b></span>Search in element's ancestor nodes
</div>
</div>
<h2>Public methods</h2>
<div class="padd">
<h3>find</h3>
<div class="padd">
Static function that returns a collection of elements that return true if passed in the given function.
<br><b>Arguments</b>
<div class="arg">
mixed <code>$context</code>Element, array of elements or collection from which to start searching<br>
function <code>$function</code>Filter function returned bt create_function. Two parameters will be passed: the element (as reference), the index of the element relative to the same level allowed elments<br>
int <code>$searchtype</code>One of the iteration type constants<br>
array <code>$allowedElements</code>Array of node type constants. Only nodes with one of this type will be included in the search. If this variable is not set every node will be included.<br>
</div>
<b>Return</b><br><span class="return">Object</span> Dom collection<br>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$context=& $document->body; //Take the body as the context
$func=create_function(
'$element,$index',
'if($element->hasAttributes()) return true; else return false;'
);
//Get all nodes with at least one attribute
$col=& HTMLFilterIterator::find($context,$func);
//Get all direct children with at least one attribute
$col2=& HTMLFilterIterator::find($context,$func,HTML_SEARCH_CHILDREN);
//Get all siblings of the context with at least one attribute
$col3=& HTMLFilterIterator::find($context,$func,HTML_SEARCH_SIBLINGS);
</pre>
<b>Example</b>
<pre class="brush: php; tab-size: 4;">
$context=& $document->body; //Take the body as the context
$func=create_function(
'$element,$index',
'if($element->textContent) return true; else return false;'
);
//Get all text nodes in the body sub tree that are not empty
$col4=& HTMLFilterIterator::find(
$context,
$func,
HTML_SEARCH_DESCENDANT,
array(TEXT_NODE)
);
</pre>
</div>
</div>
</div>
<div class="content hiddenpanel" id="credits">
Copyright 2009 Marco Marchiò<br><br>
Author: Marco Marchiò<br><br>
License:<br>This code is released under LGPL license. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
</div>
</div>
</td>
</tr>
</table>
</body>
</html>