がるの健忘録

エンジニアでゲーマーで講師で占い師なおいちゃんのブログです。

それはどうなのよ?

PHPに。simpleXMLという、XMLパーサがあるです。


http://jp2.php.net/simplexml

You can't use CDATA with SimpleXML, but there is a way around it. Wrap your child in CDATA like this:

ぱぁどん?
You can't use CDATA with SimpleXML,
あんだってをい?


いやまぁ「回避策」はいくつか、上述Pageに載ってるわけなのですが。

<? $listing->addChild( 'description', '<![CDATA[' . $row['description'] . ']]>' ); ?>

And then when you display the XML, run it through this function:

<?

function fixCDATA($string) {
    $find[]     = '&lt;![CDATA[';
    $replace[] = '<![CDATA[';
   
    $find[]     = ']]&gt;';
    $replace[] = ']]>';

    return $string = str_replace($find, $replace, $string);
}

$xml = fixCDATA( $xml->asXML() );
echo $xml;

とか

<?php
$str = <<< XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<menu>
    <item>
        <title><![CDATA[Caf&eacute;]]></title>
        <description><![CDATA[Some description.]]></description>
    </item>
</menu>
XML;

$str = preg_replace("/\<\!\[CDATA\[(.*?)\]\]\>/ies", "'[CDATA]'.base64_encode('$1').'[/CDATA]'", $str);
$xml = new SimpleXMLElement($str);

// Return item title's
foreach ($xml->item as $item) {
    $tmp = (string) $item->title;
    $tmp = preg_replace("/\[CDATA\](.*?)\[\/CDATA\]/ies", "base64_decode('$1')", $tmp);
    echo $tmp;
}

とか。


でもさぁ。でもさぁ。………いやまぁうんいいや orz