예제 #1
0
 public static function DOMElementToString($domelement, $contentOnly = false)
 {
     if (!is_a($domelement, "DOMElement")) {
         CMS_grandFather::raiseError('Domelement is not a DOMElement instance');
         return false;
     }
     static $autoClosedTagsList;
     if (!$autoClosedTagsList) {
         $xml2Array = new CMS_xml2Array();
         $tagsList = $xml2Array->getAutoClosedTagsList();
         $autoClosedTagsList = implode($tagsList, '|');
     }
     $output = '';
     if ($contentOnly) {
         $output = '';
         foreach ($domelement->childNodes as $node) {
             $output .= $node->ownerDocument->saveXML($node, LIBXML_NOEMPTYTAG);
         }
     } else {
         $output = $domNode->ownerDocument->saveXML($domNode, LIBXML_NOEMPTYTAG);
     }
     //convert output encoding if needed
     if (io::isUTF8($output)) {
         if (io::strtolower(APPLICATION_DEFAULT_ENCODING) != 'utf-8') {
             $output = utf8_decode($output);
         }
     } else {
         if (io::strtolower(APPLICATION_DEFAULT_ENCODING) == 'utf-8') {
             $output = utf8_encode($output);
         }
     }
     //to correct a bug in libXML < 2.6.27
     if (LIBXML_VERSION < 20627 && strpos($output, '&#x') !== false) {
         $output = preg_replace_callback('/(&#x[0-9A-Z]+;)/U', create_function('$matches', 'return io::decodeEntities($matches[0]);'), $output);
     }
     //replace tags like <br></br> by auto closed tags and strip cariage return arround entities
     $output = preg_replace(array('#\\n(&[a-z]+;)\\n#U', '#<(' . $autoClosedTagsList . ')([^>]*)></\\1>#U'), array('\\1', '<\\1\\2/>'), $output);
     return $output;
 }