当前位置: 首页>>代码示例>>PHP>>正文


PHP CDataXML::__stripComments方法代码示例

本文整理汇总了PHP中CDataXML::__stripComments方法的典型用法代码示例。如果您正苦于以下问题:PHP CDataXML::__stripComments方法的具体用法?PHP CDataXML::__stripComments怎么用?PHP CDataXML::__stripComments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CDataXML的用法示例。


在下文中一共展示了CDataXML::__stripComments方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: __parse

 /**
  * Will return an DOM object tree from the well formed XML.
  *
  * @param string $strXMLText
  * @return CDataXMLDocument
  */
 function __parse(&$strXMLText)
 {
     static $search = array(">", "<", "'", """, "&");
     static $replace = array(">", "<", "'", '"', "&");
     $oXMLDocument = new CDataXMLDocument();
     // strip comments
     $strXMLText =& CDataXML::__stripComments($strXMLText);
     // stip the !doctype
     // The DOCTYPE declaration can consists of an internal DTD in square brackets
     $cnt = 0;
     $strXMLText = preg_replace("%<\\!DOCTYPE[^\\[>]*\\[.*?\\]>%is", "", $strXMLText, -1, $cnt);
     if ($cnt == 0) {
         $strXMLText = preg_replace("%<\\!DOCTYPE[^>]*>%is", "", $strXMLText);
     }
     // get document version and encoding from header
     preg_match_all("#<\\?(.*?)\\?>#i", $strXMLText, $arXMLHeader_tmp);
     foreach ($arXMLHeader_tmp[0] as $strXMLHeader_tmp) {
         preg_match_all("/([a-zA-Z:]+=\".*?\")/i", $strXMLHeader_tmp, $arXMLParam_tmp);
         foreach ($arXMLParam_tmp[0] as $strXMLParam_tmp) {
             if ($strXMLParam_tmp != '') {
                 $arXMLAttribute_tmp = explode("=\"", $strXMLParam_tmp);
                 if ($arXMLAttribute_tmp[0] == "version") {
                     $oXMLDocument->version = substr($arXMLAttribute_tmp[1], 0, strlen($arXMLAttribute_tmp[1]) - 1);
                 } elseif ($arXMLAttribute_tmp[0] == "encoding") {
                     $oXMLDocument->encoding = substr($arXMLAttribute_tmp[1], 0, strlen($arXMLAttribute_tmp[1]) - 1);
                 }
             }
         }
     }
     // strip header
     $strXMLText =& preg_replace("#<\\?.*?\\?>#", "", $strXMLText);
     $oXMLDocument->root =& $oXMLDocument->children;
     /** @var CDataXMLNode $currentNode */
     $currentNode =& $oXMLDocument;
     $tok = strtok($strXMLText, "<");
     $arTag = explode(">", $tok);
     if (count($arTag) < 2) {
         //There was whitespace before <, so make another try
         $tok = strtok("<");
         $arTag = explode(">", $tok);
         if (count($arTag) < 2) {
             //It's a broken XML
             return false;
         }
     }
     while ($tok !== false) {
         $tagName = $arTag[0];
         $tagContent = $arTag[1];
         // find tag name with attributes
         // check if it's an endtag </tagname>
         if ($tagName[0] == "/") {
             $tagName = substr($tagName, 1);
             // strip out namespace; nameSpace:Name
             if ($this->delete_ns) {
                 $colonPos = strpos($tagName, ":");
                 if ($colonPos > 0) {
                     $tagName = substr($tagName, $colonPos + 1);
                 }
             }
             if ($currentNode->name != $tagName) {
                 // Error parsing XML, unmatched tags $tagName
                 return false;
             }
             $currentNode = $currentNode->_parent;
             // convert special chars
             if (!$this->TrimWhiteSpace || trim($tagContent) != "") {
                 $currentNode->content = str_replace($search, $replace, $tagContent);
             }
         } elseif (strncmp($tagName, "![CDATA[", 8) === 0) {
             //because cdata may contain > and < chars
             //it is special processing needed
             $cdata = "";
             for ($i = 0, $c = count($arTag); $i < $c; $i++) {
                 $cdata .= $arTag[$i] . ">";
                 if (substr($cdata, -3) == "]]>") {
                     $tagContent = $arTag[$i + 1];
                     break;
                 }
             }
             if (substr($cdata, -3) != "]]>") {
                 $cdata = substr($cdata, 0, -1) . "<";
                 do {
                     $tok = strtok(">");
                     //unfortunatly strtok eats > followed by >
                     $cdata .= $tok . ">";
                     //util end of string or end of cdata found
                 } while ($tok !== false && substr($tok, -2) != "]]");
                 //$tagName = substr($tagName, 0, -1);
             }
             $cdataSection = substr($cdata, 8, -3);
             // new CDATA node
             $subNode = new CDataXMLNode();
             $subNode->name = "cdata-section";
             $subNode->content = $cdataSection;
//.........这里部分代码省略.........
开发者ID:k-kalashnikov,项目名称:geekcon_new,代码行数:101,代码来源:xml.php


注:本文中的CDataXML::__stripComments方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。