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.parseText Load Methods
load()
Description
bool load ( mixed $rawText )
A method of class parseText. Parses and stores rawText. It will tokenize the provided Text into the following content types:
- space
- punctuation
- word
- other
Parameters
- rawText
- REQUIRED. A string of plain text or an array of a single parseHTML token of type “text”. The provided string, or the “value” attribute of the parseHTML token must be free of all HTML, excepting special HTML characters like
>.
Return Values
Returns TRUE upon success;
Examples
<?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>;
?>
reload()
Description
bool reload ( )
A method of class parseText. Re-parses the text. This is useful if you have injected new text into the provided plain text tokens of parseText. WARNING: tokens previously acquired through get methods (prior to calling reload) will not match new tokenization.
Return Values
Returns TRUE upon success;
Examples
<?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
?>
unload()
Description
mixed unload ( )
A method of class parseText. Reconstitutes text from tokenized parseText instance. It either outputs text as a string or as a single parseHTML token of type “text” (output will match format of input provided the load method). It also clears tokenized text from parseText instance.
Return Values
Returns reconstituted text as a string or as a single parseHTML token of type “text” (output will match format of input provided the load method) .
Examples
<?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
?>
update()
Description
bool update ( array $tokens )
A method of class parseText. Commits any edits to tokens to the parseText instance. Unlike the parseHTML class, parseText tokens are not subject to locking or unlocking.
Parameters
- tokens
- REQUIRED. Array of tokens. Tokens must be formatted according to the expected parseText format. Generally, tokens are acquired from a parseText instance using one of the many provided get methods.
Return Values
Returns TRUE upon success;
Examples
<?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
?>
clear()
Description
bool clear ( )
A method of class parseText. Deletes the tokenized contents of the current instance of parseText.
Return Values
Returns TRUE upon success;
Examples
<?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
?>

