parseHTML Load Methods

This draft of the doc­u­men­ta­tion remains imma­ture. While we have made attempt to be thor­ough and accu­rate, you may encounter errors. If you dis­cover any defi­cien­cies, please let us know at info@​kingdesk.​com

This page is a sub­set of the doc­u­men­ta­tion of the func­tion­al­ity pro­vided by the PHP Parser project.

parse­HTML Load Methods

Descrip­tion

bool load ( string $rawHTML )

A method of class parse­HTML. Parses and stores rawHTML. It will tok­enize the pro­vided HTML into the fol­low­ing con­tent types:

  • the XML declaration
  • the Doc­u­ment Type Defination
  • HTML tags
  • plain text
  • CDATA
  • HTML com­ments

Para­me­ters

rawHTML
REQUIRED. A string of valid xHTML markup. In par­tic­u­lar: every tag must be closed, every attribute must have a value enclosed in quotes, and tag names and attrib­utes are all lowercase.

Return Val­ues

Returns TRUE upon success;

Exam­ples


<?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>
?>

return to top

reload()

Descrip­tion

bool reload ( )

A method of class parse­HTML. Re-​​parses the HTML. This is use­ful if you have injected HTML markup into the plain text tokens ofparse­HTML. WARNING: All tokens will be locked after update, and tokens pre­vi­ously acquired through get meth­ods (prior to call­ing reload) will not match new tokenization.

Return Val­ues

Returns TRUE upon success;

Exam­ples


<?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
?>

return to top

unload()

Descrip­tion

string unload ( )

A method of class parse­HTML. Recon­sti­tutes HTML from tok­enized parse­HTML instance, out­puts HTML as a string and clears tok­enized HTML from parse­HTML instance.

Return Val­ues

Returns recon­sti­tuted HTML as a string.

Exam­ples


<?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>
?>

return to top

update()

Descrip­tion

bool update ( array $tokens )

A method of class parse­HTML. Com­mits any edits to tokens to the parse­HTML instance. Will NOT over­write locked tokens. All tokens are locked by default. Tokens must be unlocked prior to updat­ing. Unlock­ing occurs by explicit use of one of the pro­vided “unlock” methods.

Para­me­ters

tokens
REQUIRED. Array of tokens. Tokens must be for­mat­ted accord­ing to the expected parse­HTML for­mat. Gen­er­ally, tokens are acquired from a parse­HTML instance using one of the many pro­vided get meth­ods.

Return Val­ues

Returns TRUE upon success;

Exam­ples


<?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>
?>

return to top

clear()

Descrip­tion

bool clear ( )

A method of class parse­HTML. Deletes the tok­enized con­tents of the cur­rent instance of parse­HTML.

Return Val­ues

Returns TRUE upon success;

Exam­ples


<?php
$html = "<p>some text</p>";

include('path/to/php-parser.php');
$parsedHTML = new parseHTML();
$parsedHTML->clear();
$html = $parsedHTML->unload();

echo $html; // empty string
?>

return to top