S K
S K

Reputation: 177

How to handle curly apostrphes and curly quotes in PHP when generating xml?

My DB has some text which is probably copied and pasted from word document contains some curly quotes and curly apostrophes. PHP code is generating XML data/document with this text as one of its XML element.

This is the error I'm getting when I try to display the XMl doc

This page contains the following errors:

error on line 40 at column 1: Encoding error

Below is a rendering of the page up to the first error.

I've tried as mentioned in the post here, but it isn't working for me. Also tried

$output = iconv('UTF-8', 'ASCII//TRANSLIT', $input);

as mentioned here. This one displays the text till curly quotes or apostrophe appears. Do I need to mention any different character output format here?

Is there any function available in PHP to handle these type of special characters when generating XML document. I am using <?xml version="1.0" encoding="utf-8"?> character encoding for XML documnet

Here is some of my code

header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="utf-8"?>';

$item = mysql_fetch_object($result);
<listitems>
    <item>
        <name><?=htmlspecialchars(stripslashes($item->name))?></name>
        <details><?=htmlspecialchars(stripslashes($item->details))?></details>
        .
        .
        .
        .

    </item>
</listitems>

Upvotes: 2

Views: 1013

Answers (2)

Pekka
Pekka

Reputation: 449395

on the table it says DEFAULT CHARSET=latin1

It could be that you are fetching ISO-8859-1 data and outputting it as UTF-8. That would result in invalid characters beyond the 128 basic ASCII characters.

Try this iconv():

$output = iconv('ISO-8859-1', 'UTF-8//TRANSLIT', $input);

Upvotes: 3

bardiir
bardiir

Reputation: 14782

Try to wrap the text-nodes that contain the curly apostrophes in CDATA blocks like this:

<text><![CDATA[This is my test´s text]]></text>

That way you prevent xml viewers from parsing that text and it gets rendered correctly.

Upvotes: 0

Related Questions