Solved

Saving XML file w/ UTF-8

Posted on 2008-10-21
6
1,162 Views
Last Modified: 2008-10-21
I'm trying to save an xml file formatted in UTF-8 but it doesn't appear to be working. Here is a condensed version of my code:

$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

mysql query goes here...

$xml .= "mysql xml output";

            $folder = $_SERVER['DOCUMENT_ROOT']."/path";
            
            if(!(is_dir($folder))) {
            mkdir($folder,0777);
            }


  $tempfile = $folder."/".date("Ymd").".xml";
  $fo = fopen($tempfile, 'w') or die("Cannot create XML file.");
  fwrite($fo,utf8_decode($xml));
  fclose($fo);

For some reason, I open the file and the UTF-8 characters are not recognized (seen as question marks). In the MySQL db, the collation is set to utf8_unicode_ci. Is there a way to set the file encoding when I am opening the file or writing to it?

0
Comment
Question by:sherlock5
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22768492
why are you decoding UTF8 before writing to the file? Wouldn't you need to utf8_encode when saving?
0
 

Author Comment

by:sherlock5
ID: 22768555
i also tried utf8_encode... utf8_decode was the only option that limited the coding problem to just a question mark. I also switched the  encoding="iso-8859-1" without luck.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22768568
You seem to be decoding the utf8 before you write it to the file:

fwrite($fo,utf8_decode($xml));

Then you save it as latin1. It should be just:

fwrite($fo,$xml);

...or, if the $xml is latin1 and the file should be utf8:

fwrite($fo,utf8_encode($xml));
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sherlock5
ID: 22768818
Yeah, cxr, I have tried all of those options as well. When I try to use the function, header("Content-type: application/octet-stream; charset=UTF-8");, to output the file in a new window, the content is formatted in UTF-8... so how do I save the file as a UTF-8 formatted file?
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 250 total points
ID: 22769115
You say you open it and view it and see question marks, in what environment are you opening it? Through the web browser? If you view it in the browser and change character encoding in the browser, maybe you can identify what character encoding is in the file.

Your example says: $xml .= "mysql xml output";

I suppose the real $xml variable contains content grabbed directly from the database? Or is there any conversion involved? Is the character set used in the database utf8 (not just collation)? Execute this sql to find out:

show variables like "character_set%";
0
 

Author Comment

by:sherlock5
ID: 22769538
So, you were right cxr, I changed character_set_database to utf8... then i removed the utf8_encode... and then i opened the file in firefox and notepad, saw that the characters were showing up correctly and that the encoding was utf8. thanks for the help!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now