首页 > php教程 > php手册 > 正文

PHP cdata 处理(详细介绍)

WBOY
发布: 2016-06-13 11:42:38
原创
1443 人浏览过

当时在网上找了一个CDATA的转换器, 修改之后, 将CDATA标签给过滤掉。如下

复制代码 代码如下:


 // States:
        //
        //     'out'
        //     '        //     '        //     '        //     '        //     '        //     '        //     '        //     'in'
        //     ']'
        //     ']]'
        //
        // (Yes, the states a represented by strings.)
        //
        $state = 'out';
        $a = str_split($xml);
        $new_xml = '';
        foreach ($a AS $k => $v) {
            // Deal with "state".
            switch ( $state ) {
                case 'out':
                    if ( '                        $state = $v;
                    } else {
                        $new_xml .= $v;
                    }
                break;
                case '                    if ( '!' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                 case '                    if ( '[' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '                    if ( 'C' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '                    if ( 'D' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '                    if ( 'A' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '                    if ( 'T' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '                    if ( 'A' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '                    if ( '[' == $v  ) {
                        $cdata = '';
                        $state = 'in';
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case 'in':
                    if ( ']' == $v ) {
                        $state = $v;
                    } else {
                        $cdata .= $v;
                    }
                break;
                case ']':
                    if (  ']' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $cdata .= $state . $v;
                        $state = 'in';
                    }
                break;
                case ']]':
   if (  '>' == $v  ) {
    $new_xml .= htmlentities($cdata);
#       $new_xml.= $cdata;
//                        $new_xml .= str_replace('>','>',
  //                                  str_replace('>','    //                                str_replace('"','"',
      //                              str_replace('&','&',
        //                            $cdata))));
                        $state = 'out';
                    } else {
                        $cdata .= $state . $v;
                        $state = 'in';
                    }
                break;
            } // switch
        }
        //
        // Return.
        //
            return $new_xml;


最近发现,总是有alert发出来, 说是simplexml解析出错。

发现是原来有xml的数据是. 出现了连续的3个], 造成上面的解析函数不能处理。

而且这个问题很难修正, 你不知道下次会不会有4, 5个]出现。

所以决定还是将这段解析 的代码换成DOM XML,本身 DOM的处理还是比较简单的,

包含DOMElement, DOMDocument, DOMNodeList, DOMNode几个 component.

对于 DOMNode有nodeValue, nodeType, nodeName的成员函数。

首先先用loadXML将string转化为DOMDocument对像, 再用getElementsByTagName转化为DOMNodeList对像, 再使用->item(0)转化为DOMNOde, 然后就可以使用上面的三种方法了。

对于 test这种xml标签, 要使用 attribute函数。
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!