parseText 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­Text Load Methods

load()

Descrip­tion

bool load ( mixed $rawText )

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

  • space
  • punc­tu­a­tion
  • word
  • other

Para­me­ters

raw­Text
REQUIRED. A string of plain text or an array of a sin­gle parse­HTML token of type “text”. The pro­vided string, or the “value” attribute of the parse­HTML token must be free of all HTML, except­ing spe­cial HTML char­ac­ters like >.

Return Val­ues

Returns TRUE upon success;

Exam­ples


<?php
$text = "sample text and an email@example.com";

include('path/to/php-parser.php');
$parsedText = new parseText();
$parsedText->load($text);
$words = $parsedText->get_words();
foreach($words as &$word) {
	$word["value"] = strtoupper($word["value"]);
}
$parsedText->update($words);
$text = $parsedText->unload();

echo $text; // SAMPLE TEXT AND AN email@example.com
?>

<?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);
	$words = $parsedText->get_words();
	foreach($words as &$word) {
		$word["value"] = strtoupper($word["value"]);
	}
	$parsedText->update($words);
	$unlockedText = $parsedText->unload();
}

$parsedHTML->update($unlockedTexts);
$html = $parsedHTML->unload();

echo $html; // <p>GO TO http://example.com.</p>;
?>

return to top

reload()

Descrip­tion

bool reload ( )

A method of class parse­Text. Re-​​parses the text. This is use­ful if you have injected new text into the pro­vided plain text tokens of parse­Text. WARNING: 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
$text = "sample text";

include('path/to/php-parser.php');
$parsedText = new parseText();
$parsedText->load($text);
$words = $parsedText->get_words();
foreach($words as &$word) {
	$word["value"] = $word["value"]." foo";
}
$parsedText->update($words);
$parsedText->reload();
$words = $parsedText->get_words();
foreach($words as &$word) {
	$word["value"] = $word["value"]." bar";
}
$parsedText->update($words);
$text = $parsedText->unload();
echo $text; // sample bar foo bar text bar foo bar
?>

return to top

unload()

Descrip­tion

mixed unload ( )

A method of class parse­Text. Recon­sti­tutes text from tok­enized parse­Text instance. It either out­puts text as a string or as a sin­gle parse­HTML token of type “text” (out­put will match for­mat of input pro­vided the load method). It also clears tok­enized text from parse­Text instance.

Return Val­ues

Returns recon­sti­tuted text as a string or as a sin­gle parse­HTML token of type “text” (out­put will match for­mat of input pro­vided the load method) .

Exam­ples


<?php
$text = "sample text and an email@example.com";

include('path/to/php-parser.php');
$parsedText = new parseText();
$parsedText->load($text);
$words = $parsedText->get_words();
foreach($words as &$word) {
	$word["value"] = strtoupper($word["value"]);
}
$parsedText->update($words);
$text = $parsedText->unload();

echo $text; // SAMPLE TEXT AND AN email@example.com
?>

return to top

update()

Descrip­tion

bool update ( array $tokens )

A method of class parse­Text. Com­mits any edits to tokens to the parse­Text instance. Unlike the parse­HTML class, parse­Text tokens are not sub­ject to lock­ing or unlocking.

Para­me­ters

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

Return Val­ues

Returns TRUE upon success;

Exam­ples


<?php
$text = "sample text and an email@example.com";

include('path/to/php-parser.php');
$parsedText = new parseText();
$parsedText->load($text);
$words = $parsedText->get_words();
foreach($words as &$word) {
	$word["value"] = strtoupper($word["value"]);
}
$parsedText->update($words);
$text = $parsedText->unload();

echo $text; // SAMPLE TEXT AND AN email@example.com
?>

return to top

clear()

Descrip­tion

bool clear ( )

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

Return Val­ues

Returns TRUE upon success;

Exam­ples


<?php
$text = "sample text and an email@example.com";

include('path/to/php-parser.php');
$parsedText = new parseText();
$parsedText->load($text);
$parsedText->clear();
$text = $parsedText->unload();

echo $text; // NULL
?>

return to top