PHP CDATA Processing (details) _php tutorial

Source: Internet
Author: User
Tags cdata
A CDATA Converter was found on the Internet, and after the modification, the CDATA label was filtered out. As follows
Copy CodeThe code is as follows:
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 (' < ' = = $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 ' 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;

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

found that the data that originally had XML was<![cdata[domain[test]] (3 consecutive], resulting in the above parsing function cannot be processed. <br> <br> and the problem is hard to fix, you don't know if there will be 4 or 5 next time. <br> <br> So the decision to replace the parsed code with Dom XML, the DOM itself is still relatively simple to handle, <br> <br> contains DomElement, DOMDocument, Domnodelist, domnode several component . <br> <br> has nodevalue, NodeType, and nodename member functions for Domnode. <br> <br> First converts a string into a DOMDocument pair with Loadxml, getElementsByTagName to Domnodelist, and then->item (0) into Domnode, Then you can use the three methods above. <br> <br> Use the attribute function for the XML tag of test </aa>. </p> <p align= "left" ><span id= "url" itemprop= "url" >http://www.bkjia.com/phpjc/328068.html </span> <span id= " Indexurl "itemprop=" Indexurl ">www.bkjia.com </span> <span id=" isoriginal "itemprop=" isoriginal ">true </span> <span Id= "Isbasedonurl" itemprop= "Isbasedonurl" >http://www.bkjia.com/phpjc/328068.html </span> <span id= "Genre" itemprop= "genre" >techarticle </span> <span id= "description" itemprop= "description" > at the time, I found a CDATA converter on the Internet, and after the modification, we filtered the CDATA tag. The following copy code code is as follows://states:///' out '//'/'! '//'! [' // '! [C '/... </span> </p> <li ><i class= "Layui-icon" >& #xe63a;]]>

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.