function getTextBetweenTags($tag, $html, $strict = 0)
{
/*** a new dom object ***/
//$dom = new domDocument;
$dom = new domDocument('1.0', 'UTF-8');
/*** load the html into the object ***/
$html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
@$dom->loadHTML($html);
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the tag by its tag name ***/
$content = $dom->getElementsByTagname($tag);
/*** the array to return ***/
////$out = array();
$out = "";
foreach ($content as $item) {
/*** add node value to the out array ***/
$out .= "\"" . $item->nodeValue . "\", ";
}
/*** return the results ***/
return $out;
}
/**
*
* @get text between tags
*
* @param string $tag The tag name
*
* @param string $html The XML or XHTML string
*
* @param int $strict Whether to use strict mode
*
* @return array
*
*/
function getTextBetweenTags($tag, $html, $strict = 0)
{
/*** a new dom object ***/
$dom = new domDocument();
/*** load the html into the object ***/
if ($strict == 1) {
$dom->loadXML($html);
} else {
$dom->loadHTML($html);
}
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the tag by its tag name ***/
$content = $dom->getElementsByTagname($tag);
/*** the array to return ***/
$out = array();
foreach ($content as $item) {
/*** add node value to the out array ***/
$out[] = $item->nodeValue;
}
/*** return the results ***/
return $out;
}
echo "--------- Remove Attribute Node\n";
$attr = $rootnode->removeAttribute("src");
print "Removed " . $attr . " attributes.\n";
echo "--------- attributes of rootnode\n";
$attrs = $rootnode->attributes;
print_node_list($attrs);
echo "--------- children of an attribute\n";
$children = $attrs->item(0)->childNodes;
print_node_list($children);
echo "--------- Add child to root\n";
$myelement = new domElement("Silly", "Symphony");
$newchild = $rootnode->appendChild($myelement);
print_node($newchild);
print $dom->saveXML();
print "\n";
echo "--------- Find element by tagname\n";
echo " Using dom\n";
$children = $dom->getElementsByTagname("Silly");
print_node_list($children);
echo " Using elem\n";
$children = $rootnode->getElementsByTagName("Silly");
print_node_list($children);
echo "--------- Unlink Node\n";
print_node($children->item(0));
$rootnode->removeChild($children->item(0));
print_node_list($rootnode->childNodes);
print $dom->savexml();
echo "--------- Find element by id\n";
print "Not implemented\n";
echo "--------- Check various node_name return values\n";
print "Not needed\n";
/**
* Returns the text between 2 tags
*
* @return array
* @param string $tag The tag.
* @param string $html The HTML to search in.
* @param bool[optional] $strict Use strictmode?
*/
private function getTextBetweenTags($tag, $html, $strict = false)
{
// new dom document
$dom = new domDocument();
// load HTML
$strict == true ? $dom->loadXML($html) : $dom->loadHTML($html);
// discard whitespace
$dom->preserveWhiteSpace = false;
// the array with results
$results = array();
// fetch the tag by name
$content = $dom->getElementsByTagname($tag);
// loop the content
foreach ($content as $item) {
// add node value to results
$results[] = $item->nodeValue;
}
// return the results
return $results;
}
/**
*
* @get text between tags
* @param string $tag The tag name
* @param string $html The XML or XHTML string
* @param int $strict Whether to use strict mode
* @param string $encoding
* @return array
*/
private function getTextBetweenTags($tag, $html, $strict = 0, $encoding = "UTF-8")
{
global $PAGE, $CFG;
if (!isset($CFG->filter_jsxgraph_divid)) {
set_config('filter_jsxgraph_divid', 'box');
}
if (!isset($CFG->filter_jsxgraph_boardvar)) {
set_config('filter_jsxgraph_boardvar', 'board');
}
if (!isset($CFG->filter_jsxgraph_width)) {
set_config('filter_jsxgraph_width', '500');
}
if (!isset($CFG->filter_jsxgraph_height)) {
set_config('filter_jsxgraph_height', '400');
}
// a new dom object
$dom = new domDocument();
$dom->formatOutput = true;
// load the html into the object
if ($strict == 1) {
$dom->loadXML($html);
} else {
libxml_use_internal_errors(true);
$htmlutf8 = mb_convert_encoding($html, 'HTML-ENTITIES', $encoding);
$dom->loadHTML($htmlutf8);
libxml_use_internal_errors(false);
}
// discard white space
$dom->preserveWhiteSpace = false;
$dom->strictErrorChecking = false;
$dom->recover = true;
// the tag by its tag name
$content = $dom->getElementsByTagname($tag);
if (count($content) > 0) {
$PAGE->requires->js(new moodle_url($CFG->wwwroot . '/filter/jsxgraph/jsxgraphcore.js'));
}
// Iterate backwards through the jsxgraph tags
$i = $content->length - 1;
while ($i > -1) {
$item = $content->item($i);
// Read attributes
$w = $item->getAttribute('width');
if ($w == "") {
$w = $CFG->filter_jsxgraph_width;
}
$h = $item->getAttribute('height');
if ($h == "") {
$h = $CFG->filter_jsxgraph_height;
}
$b = $item->getAttribute('box');
if ($b == "") {
$b = $CFG->filter_jsxgraph_divid . $i;
}
$brd = $item->getAttribute('board');
if ($brd == "") {
$brd = $CFG->filter_jsxgraph_boardvar . $i;
}
/* Create new div element containing JSXGraph */
$out = $dom->createElement('div');
$a = $dom->createAttribute('id');
$a->value = $b;
$out->appendChild($a);
$a = $dom->createAttribute('class');
$a->value = "jxgbox";
$out->appendChild($a);
$a = $dom->createAttribute('style');
$a->value = "width:" . $w . "px; height:" . $h . "px; ";
$out->appendChild($a);
$t = $dom->createTextNode("");
$out->appendChild($t);
$out = $dom->appendChild($out);
// Replace <jsxgraph> by <div>
$item->parentNode->replaceChild($out, $item);
$code = "";
$needGXT = false;
$url = $item->getAttribute('file');
if ($url != "") {
$code = "var " . $brd . " = JXG.JSXGraph.loadBoardFromFile('" . $b . "', '" . $url . "', 'Geonext');";
$needGXT = true;
} else {
$url = $item->getAttribute('filestring');
if ($url != "") {
$code = "var " . $brd . " = JXG.JSXGraph.loadBoardFromString('" . $b . "', '" . $url . "', 'Geonext');";
$needGXT = true;
} else {
// Plain JavaScript code
$code = $item->nodeValue;
}
}
// Place JavaScript code at the end of the page.
$PAGE->requires->js_init_call($code);
if ($needGXT) {
$PAGE->requires->js(new moodle_url($CFG->wwwroot . '/filter/jsxgraph/GeonextReader.js'));
}
--$i;
}
// remove DOCTYPE
$dom->removeChild($dom->firstChild);
// remove <html><body></body></html>
//$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
//return $dom->saveXML();
$str = $dom->saveHTML();
$str = str_replace("<body>", "", $str);
$str = str_replace("</body>", "", $str);
$str = str_replace("<html>", "", $str);
$str = str_replace("</html>", "", $str);
return $str;
}
function tagwrap($html, $width = 75, $break = "nr")
{
$html = '<div>' . $html . '</div>';
//using dom object
$dom = new domDocument();
//load the html into the object
$dom->loadXML($html);
// preserve white space
$dom->preserveWhiteSpace = true;
//getting all tags
$content = $dom->getElementsByTagname("*");
$html = "";
foreach ($content as $item) {
//wrapping contents of tags, function described above is used,
//but you can use your own function or simple wordwrap() php function
$item->nodeValue = mb_wordwrap($item->nodeValue, $width, $break);
$html .= $dom->saveXML($item);
}
//return the results
return $html;
//html_entity_decode($html);
}