Posted By

wza on 12/27/10


Tagged

textmate cdata simplexml uncdata


Versions (?)

uncdata


 / Published in: PHP
 

  1. function uncdata($xml)
  2. {
  3. // States:
  4. //
  5. // 'out'
  6. // '<'
  7. // '<!'
  8. // '<!['
  9. // '<![C'
  10. // '<![CD'
  11. // '<![CDAT'
  12. // '<![CDATA'
  13. // 'in'
  14. // ']'
  15. // ']]'
  16. //
  17. // (Yes, the states a represented by strings.)
  18. //
  19.  
  20. $state = 'out';
  21.  
  22. $a = str_split($xml);
  23.  
  24. $new_xml = '';
  25.  
  26. foreach ($a AS $k => $v) {
  27.  
  28. // Deal with "state".
  29. switch ( $state ) {
  30. case 'out':
  31. if ( '<' == $v ) {
  32. $state = $v;
  33. } else {
  34. $new_xml .= $v;
  35. }
  36. break;
  37.  
  38. case '<':
  39. if ( '!' == $v ) {
  40. $state = $state . $v;
  41. } else {
  42. $new_xml .= $state . $v;
  43. $state = 'out';
  44. }
  45. break;
  46.  
  47. case '<!':
  48. if ( '[' == $v ) {
  49. $state = $state . $v;
  50. } else {
  51. $new_xml .= $state . $v;
  52. $state = 'out';
  53. }
  54. break;
  55.  
  56. case '<![':
  57. if ( 'C' == $v ) {
  58. $state = $state . $v;
  59. } else {
  60. $new_xml .= $state . $v;
  61. $state = 'out';
  62. }
  63. break;
  64.  
  65. case '<![C':
  66. if ( 'D' == $v ) {
  67. $state = $state . $v;
  68. } else {
  69. $new_xml .= $state . $v;
  70. $state = 'out';
  71. }
  72. break;
  73.  
  74. case '<![CD':
  75. if ( 'A' == $v ) {
  76. $state = $state . $v;
  77. } else {
  78. $new_xml .= $state . $v;
  79. $state = 'out';
  80. }
  81. break;
  82.  
  83. case '<![CDA':
  84. if ( 'T' == $v ) {
  85. $state = $state . $v;
  86. } else {
  87. $new_xml .= $state . $v;
  88. $state = 'out';
  89. }
  90. break;
  91.  
  92. case '<![CDAT':
  93. if ( 'A' == $v ) {
  94. $state = $state . $v;
  95. } else {
  96. $new_xml .= $state . $v;
  97. $state = 'out';
  98. }
  99. break;
  100.  
  101. case '<![CDATA':
  102. if ( '[' == $v ) {
  103.  
  104.  
  105. $cdata = '';
  106. $state = 'in';
  107. } else {
  108. $new_xml .= $state . $v;
  109. $state = 'out';
  110. }
  111. break;
  112.  
  113. case 'in':
  114. if ( ']' == $v ) {
  115. $state = $v;
  116. } else {
  117. $cdata .= $v;
  118. }
  119. break;
  120.  
  121. case ']':
  122. if ( ']' == $v ) {
  123. $state = $state . $v;
  124. } else {
  125. $cdata .= $state . $v;
  126. $state = 'in';
  127. }
  128. break;
  129.  
  130. case ']]':
  131. if ( '>' == $v ) {
  132. $new_xml .= str_replace('>','&gt;',
  133. str_replace('>','&lt;',
  134. str_replace('"','&quot;',
  135. str_replace('&','&amp;',
  136. $cdata))));
  137. $state = 'out';
  138. } else {
  139. $cdata .= $state . $v;
  140. $state = 'in';
  141. }
  142. break;
  143. } // switch
  144.  
  145. }
  146.  
  147. //
  148. // Return.
  149. //
  150. return $new_xml;
  151.  
  152. }

Report this snippet  

You need to login to post a comment.