PHP CDATA Processing (detailed) _php tips

Source: Internet
Author: User
Tags cdata
A CDATA Converter was found on the Internet, and after modification, the CDATA label was filtered out. As follows
Copy Code code as follows:

States:
//
' Out '
' < '
' <! '
' <! ['
' <! C
' <! [CD '
' <! [Cdat '
' <! [CDATA '
' 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 (' < ' = $v) {
$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 ' <! [C ':
if (' D ' = = $v) {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! [CD ':
if (' A ' = = $v) {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! [CDA ':
if (' T ' = = $v) {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! [Cdat ':
if (' A ' = = $v) {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! [CDATA ':
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;

Recently found that there is always alert sent to say that SimpleXML parsing error.

found that the original XML data is <! [Cdata[domain[test]]]. There are 3 consecutive occurrences, resulting in the above parsing function not being processed.

And the problem is hard to fix, and you don't know if there will be 4 or 5 next time.

So decide to change this parsing code into DOM XML, the processing of the DOM itself is relatively simple,

Contains DomElement, DOMDocument, Domnodelist, domnode several component.

For Domnode there are nodevalue, NodeType, and nodename member functions.

First, we use Loadxml to convert string to DOMDocument, then convert getElementsByTagName to domnodelist image, then use->item (0) to convert to Domnode, Then you can use the three methods above.

For <aa color= ' red ' >test</aa> this XML tag, use the attribute function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.