PHP: Writing non-english characters to XML - encoding problem
- by Dean
Hello,
I wrote a small PHP script to edit the site news XML file.
I used DOM to manipulate the XML (Loading, writing, editing).
It works fine when writing English characters, but when non-English characters are written, PHP throws an error when trying to load the file.
If I manually type non-English characters into the file - it's loaded perfectly fine, but if PHP writes the non-English characters the encoding goes wrong, although I specified the utf-8 encoding.
Any help is appreciated.
Errors:
Warning: DOMDocument::load()
[domdocument.load]: Entity 'times' not
defined in filepath
Warning: DOMDocument::load()
[domdocument.load]: Input is not
proper UTF-8, indicate encoding !
Bytes: 0x91 0x26 0x74 0x69 in filepath
Here are the functions responsible for loading and saving the file (self-explanatory):
function get_tags_from_xml(){
// Load news entries from XML file for display
$errors = Array();
if(!$xml_file = load_news_file()){
// Load file
// String indicates error presence
$errors = "file not found";
return $errors;
}
$taglist = $xml_file->getElementsByTagName("text");
return $taglist;
}
function set_news_lang(){
// Sets the news language
global $news_lang;
if($_POST["news-lang"]){
$news_lang = htmlentities($_POST["news-lang"]);
}
elseif($_GET["news-lang"]){
$news_lang = htmlentities($_GET["news-lang"]);
}
else{
$news_lang = "he";
}
}
function load_news_file(){
// Load XML news file for proccessing, depending on language
global $news_lang;
$doc = new DOMDocument('1.0','utf-8');
// Create new XML document
$doc->load("news_{$news_lang}.xml");
// Load news file by language
$doc->formatOutput = true;
// Nicely format the file
return $doc;
}
function save_news_file($doc){
// Save XML news file, depending on language
global $news_lang;
$doc->saveXML($doc->documentElement);
$doc->save("news_{$news_lang}.xml");
}
Here is the code for writing to XML (add news):
<?php ob_start()?>
<?php include("include/xml_functions.php")?>
<?php include("../include/functions.php")?>
<?php get_lang();?>
<?php
//TODO: ADD USER AUTHENTICATION!
if(isset($_POST["news"]) && isset($_POST["news-lang"])){
set_news_lang();
$news = htmlentities($_POST["news"]);
$xml_doc = load_news_file();
$news_list = $xml_doc->getElementsByTagName("text");
// Get all existing news from file
$doc_root_element = $xml_doc->getElementsByTagName("news")->item(0);
// Get the root element of the new XML document
$new_news_entry = $xml_doc->createElement("text",$news);
// Create the submited news entry
$doc_root_element->appendChild($new_news_entry);
// Append submited news entry
$xml_doc->appendChild($doc_root_element);
save_news_file($xml_doc);
header("Location: /cpanel/index.php?lang={$lang}&news-lang={$news_lang}");
}
else{
header("Location: /cpanel/index.php?lang={$lang}&news-lang={$news_lang}");
}
?>
<?php ob_end_flush()?>