You are here

function hotpot_xml_tree::encode_cdata in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/question/format/hotpot/format.php \hotpot_xml_tree::encode_cdata()
1 call to hotpot_xml_tree::encode_cdata()
hotpot_xml_tree::hotpot_xml_tree in includes/moodle/question/format/hotpot/format.php

File

includes/moodle/question/format/hotpot/format.php, line 591

Class

hotpot_xml_tree

Code

function encode_cdata(&$str, $tag) {

  // conversion tables
  static $HTML_ENTITIES = array(
    ''' => "'",
    '"' => '"',
    '&lt;' => '<',
    '&gt;' => '>',
    '&amp;' => '&',
  );
  static $ILLEGAL_STRINGS = array(
    "\r" => '',
    "\n" => '&lt;br /&gt;',
    ']]>' => '&#93;&#93;&#62;',
  );

  // extract the $tag from the $str(ing), if possible
  $pattern = '|(^.*<' . $tag . '[^>]*)(>.*<)(/' . $tag . '>.*$)|is';
  if (preg_match($pattern, $str, $matches)) {

    // encode problematic CDATA chars and strings
    $matches[2] = strtr($matches[2], $ILLEGAL_STRINGS);

    // if there are any ampersands in "open text"
    // surround them by CDATA start and end markers
    // (and convert HTML entities to plain text)
    $search = '/>([^<]*&[^<]*)</e';
    $replace = '"><![CDATA[".strtr("$1", $HTML_ENTITIES)."]]><"';
    $matches[2] = preg_replace($search, $replace, $matches[2]);
    $str = $matches[1] . $matches[2] . $matches[3];
  }
}