This draft of the documentation remains immature. While we have made attempt to be thorough and accurate, you may encounter errors. If you discover any deficiencies, please let us know at info@kingdesk.com
This page is a subset of the documentation of the functionality provided by the PHP Parser project.parseHTML Load Methods
Description
bool load ( string $rawHTML )
A method of class parseHTML. Parses and stores rawHTML. It will tokenize the provided HTML into the following content types:
- the XML declaration
- the Document Type Defination
- HTML tags
- plain text
- CDATA
- HTML comments
Parameters
- rawHTML
- REQUIRED. A string of valid xHTML markup. In particular: every tag must be closed, every attribute must have a value enclosed in quotes, and tag names and attributes are all lowercase.
Return Values
Returns TRUE upon success;
Examples
<?php
$html = "<p>some text</p>";
include('path/to/php-parser.php');
$parsedHTML = new parseHTML();
$parsedHTML->load($html);
$parsedHTML->unlock_text();
$unlockedTexts = $parsedHTML->get_unlocked_text();
foreach($unlockedTexts as &$unlockedText) {
// do something here like... SHOUT!
$unlockedText["value"] = strtoupper($unlockedText["value"]);
}
$parsedHTML->update($unlockedTexts);
$html = $parsedHTML->unload();
echo $html; // <p>some text</p>
?>
reload()
Description
bool reload ( )
A method of class parseHTML. Re-parses the HTML. This is useful if you have injected HTML markup into the plain text tokens ofparseHTML. WARNING: All tokens will be locked after update, and tokens previously acquired through get methods (prior to calling reload) will not match new tokenization.
Return Values
Returns TRUE upon success;
Examples
<?php
$html = "<p>some text</p>";
include('path/to/php-parser.php');
$parsedHTML = new parseHTML();
$parsedHTML->load($html);
$parsedHTML->unlock_text();
$unlockedTexts = $parsedHTML->get_unlocked_text();
foreach($unlockedTexts as &$unlockedText) {
$unlockedText["value"] = "<em>".$unlockedText["value"]"</em>";
}
$parsedHTML->update($unlockedTexts);
$parsedHTML->reload();
// all text values are now free of the recently added <em> tags
?>
unload()
Description
string unload ( )
A method of class parseHTML. Reconstitutes HTML from tokenized parseHTML instance, outputs HTML as a string and clears tokenized HTML from parseHTML instance.
Return Values
Returns reconstituted HTML as a string.
Examples
<?php
$html = "<p>Go to http://example.com.</p>";
include('path/to/php-parser.php');
$parsedHTML = new parseHTML();
$parsedHTML->load($html);
$parsedHTML->unlock_text();
$unlockedTexts = $parsedHTML->get_unlocked_text();
foreach($unlockedTexts as &$unlockedText) {
$parsedText = new parseText();
$parsedText->load($unlockedText);
$urls = $parsedText->get_urls();
foreach($urls as &$url) {
$url["value"] = '<a href="'.$url["value"].'">'.$url["value"].'</a>';
}
$parsedText->update($words);
$unlockedText = $parsedText->unload();
}
$parsedHTML->update($unlockedTexts);
$html = $parsedHTML->unload();
echo $html; // <p>Go to <a href="http://example.com">http://example.com</a>.</p>
?>
update()
Description
bool update ( array $tokens )
A method of class parseHTML. Commits any edits to tokens to the parseHTML instance. Will NOT overwrite locked tokens. All tokens are locked by default. Tokens must be unlocked prior to updating. Unlocking occurs by explicit use of one of the provided “unlock” methods.
Parameters
- tokens
- REQUIRED. Array of tokens. Tokens must be formatted according to the expected parseHTML format. Generally, tokens are acquired from a parseHTML instance using one of the many provided get methods.
Return Values
Returns TRUE upon success;
Examples
<?php
$html = "<p>some text</p>";
include('path/to/php-parser.php');
$parsedHTML = new parseHTML();
$parsedHTML->load($html);
$parsedHTML->unlock_text();
$unlockedTexts = $parsedHTML->get_unlocked_text();
foreach($unlockedTexts as &$unlockedText) {
// do something here like... SHOUT!
$unlockedText["value"] = strtoupper($unlockedText["value"]);
}
$parsedHTML->update($unlockedTexts);
$html = $parsedHTML->unload();
echo $html; // <p>some text</p>
?>
clear()
Description
bool clear ( )
A method of class parseHTML. Deletes the tokenized contents of the current instance of parseHTML.
Return Values
Returns TRUE upon success;
Examples
<?php
$html = "<p>some text</p>";
include('path/to/php-parser.php');
$parsedHTML = new parseHTML();
$parsedHTML->clear();
$html = $parsedHTML->unload();
echo $html; // empty string
?>

