parseHTML Conditional 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

return to top

in_​tag()

Descrip­tion

bool in_tag ( mixed $tagNames , array $token )

A method of class parse­HTML. Tests whether token is a child of one of the spec­i­fied HTML elements.

Para­me­ters

tag­Names
REQUIRED. A string or array of HTML ele­ment name(s).
token
REQUIRED. Array of a sin­gle token. Token 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 if the pro­vided token is a child of at least one of the spec­i­fied tag­Names, FALSE if it is not.

Exam­ples


<?php
$html = "<p>some <span>text</span></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!
	if($parsedHTML->in_tag('span', $unlockedText)):
	$unlockedText["value"] = strtoupper($unlockedText["value"]);
	endif;
}
$parsedHTML->update($unlockedTexts);
$html = $parsedHTML->unload();

echo $html; // <p>some <span>TEXT</span></p>
?>

return to top

in_​id()

Descrip­tion

bool in_id ( mixed $idNames , array $token )

A method of class parse­HTML. Tests whether token is a child of an HTML ele­ment of a given ID.

Para­me­ters

idNames
REQUIRED. A string or array of ID name(s) for HTML elements.
token
REQUIRED. Array of a sin­gle token. Token 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 if the pro­vided token is a child of at least one HTML ele­ment of a given ID, FALSE if it is not.

Exam­ples


<?php
$html = "<p>some <span id="foo">text</span></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!
	if($parsedHTML->in_id('foo', $unlockedText)):
	$unlockedText["value"] = strtoupper($unlockedText["value"]);
	endif;
}
$parsedHTML->update($unlockedTexts);
$html = $parsedHTML->unload();

echo $html; // <p>some <span id="foo">TEXT</span></p>
?>

return to top

in_​class()

Descrip­tion

bool in_class ( mixed $classNames , array $token )

A method of class parse­HTML. Tests whether token is a child of an HTML ele­ment of a given class name.

Para­me­ters

class­Names
REQUIRED. A string or array of class name(s) for HTML elements.
token
REQUIRED. Array of a sin­gle token. Token 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 if the pro­vided token is a child of at least one HTML ele­ment of a given class, FALSE if it is not.

Exam­ples


<?php
$html = "<p>some <span class="foo">text</span></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!
	if($parsedHTML->in_class('foo', $unlockedText)):
	$unlockedText["value"] = strtoupper($unlockedText["value"]);
	endif;
}
$parsedHTML->update($unlockedTexts);
$html = $parsedHTML->unload();

echo $html; // <p>some <span class="foo">TEXT</span></p>
?>

return to top

in_​attribute()

Descrip­tion

bool in_attribute ( string $attributeName , mixed $attributeValues , array $token )

A method of class parse­HTML. Tests whether token is a child of an HTML ele­ment with an attribute of a given value.

Para­me­ters

attribut­e­Name
REQUIRED. A string of the name of an HTML ele­ment attribute.
attrib­ut­e­Val­ues
REQUIRED. A string or array of val­ues for the spec­i­fied HTML ele­ment attribute.
token
REQUIRED. Array of a sin­gle token. Token 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 if the pro­vided token is a child of at least one HTML ele­ment with an attribute of spe­cific value, FALSE if it is not.

Exam­ples


<?php
$html = "<p>some <span title="foo">text</span></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!
	if($parsedHTML->in_attribute('title', 'foo', $unlockedText)):
	$unlockedText["value"] = strtoupper($unlockedText["value"]);
	endif;
}
$parsedHTML->update($unlockedTexts);
$html = $parsedHTML->unload();

echo $html; // <p>some <span title="foo">TEXT</span></p>
?>

return to top