multilanguage support to xml text search php - php

I have a xml file which can be in any language(finnish, italian, swedish, dutch) I have saved the xml using headers
<?xml version="1.0" encoding="ISO-8859-1"?>
The saved xml contains special characters and some html codes as
⁏ for single code etc.
Now I want to provide a search text functionality using this xml as source as follows
$xml->xpath("//page[data[contains(., '".strtoupper($string)."')]]")
Where am strggling is that from php when I try to provide the $search_text as variable it's not matching these ⁏ and producing error
for e.g. the word nell’Esercizio is there as nell’Esercizio in xml and hence my search result is empty for xpath.
I tried htmlentities and htmlspecialchars but no luck. For special characters i tried utf8_encode(), utf8_decode() combination and it worked (for finnish language) but for these html characters it's failing.
What should be the proper way of searching text in a xml file in diff language via a php application ?

The Xpath expression has to be UTF-8, the encoding of the document is not relevant. DOM uses UTF-8 and converts on load/save. I think your problem is the strtoupper(). You need to use unicode save transliterations.
ext/intl
ext/mbstring

Related

Problems with php xml characters

Hello friends i have a problem with some characters reading a xml file from php i am using this source code:
$file = 'test.xml';
$xml_1 = simplexml_load_file($file);
echo ($xml_1->content);
its work ok but when the content is a special character like ñ ó it show a rarer character like this ñ i tried to include in html head utf8 charset but its the same
SimpleXML emits UTF-8 output by design. If you application does not support UTF-8 you'll have to convert with the usual tools (e.g. mb_convert_encoding()) but you need to take this into account:
You need to know for sure the encoding your app is using.
UTF-8 can hold the complete Unicode catalogue thus some characters may not have an equivalent in your target encoding.
Whatever, in 2016 there's no reason to use anything else than UTF-8 unless your maintaining legacy code.
Finally i find the solution i must to use utf8_decode php function to convert the characters it is not enought with put utf8 charset in the head page you must to convert using php before

What is the best way to deal with XML that contains invalid characters (PHP)?

I am integrating with Quickbooks using QBXML. I am running a customer query and the XML that Quickbooks returns appears to contain an invalid character (!).
Looking at the source XML that quickbooks returns, I can see the invalid character (actual named changed for privacy reasons, but I left in the character in question):
<Contact>Ongél Davabond</Contact>
When I try to parse the XML (with the PHP XML parser, starting with xml_parser_create() ), I get an invalid character message.
I noticed that the XML header is just:
<?xml version="1.0" ?>
I tried preg_replacing that with
<?xml version="1.0" encoding="utf-8" ?>
but that didn't make any difference.
Given that I can't change how I receive the XML, how do I best deal with it on my end? Is there a way to have the PHP XML parser accept such characters? Does PHP have a way to convert any invalid characters into their &#nnn; equivalents, without affecting the XML structure, or do I need to go through the whole of the XML character by character looking for invalid characters and replacing them manually? I have no idea what other invalid characters might come up in the future, so I am after a way to deal with all the possibilities in one go, rather than just fixing this one 'é' character.
Although I was expecting UTF-8, the XML returned was ISO-8859-1. Forcing ISO-8859-1 encoding solved the issue.

How to write a file in UTF-8 format

I have read a XML file with the simplexml_load_file() function. I suppose this function is well written and supports XML encodings correctly. My XML file is in UTF-8 format, i.e. it contains normal ANSI characters along with national characters with multibyte encoding.
So, now I want to write the XML file back with fopen() and fwrite(), also in UTF-8 format.
Should I perform some conversions to do that correctly?
Suppose variable $a contains some UTF-8 encodings. Will it be written correctly?
if your xml is really encoded in utf8 already, then the strings produced by simplexml will be utf8 also. meaning- just write them to the file as-is.
php's interfacing with the underlying libxml is a bit funky though. Even though the xml may be utf8 encoded, make sure that the xml starts with a proper encoding declaration or it may get misinterpreted.
<?xml version="1.0" encoding="UTF-8"?>

PHP 5, XSL and The Character Ú

Im having dificulty getting the letter
Ú
to render through PHP 5.3 and XSL. Its part of a string in a database and that is loaded into an XML node within a tags. However it causes the XSL/XML transformation to not render. Removing the character from the string fixes the problem instantly.
Any ideas?
What character encoding are you using? From the sounds of it you have some sort of character encoding mismatch.
If your XSL is using ISO-8559-1 (or ASCII equivalent) and you are trying to output to a page that is UTF-8 encoded then the character output will be off. It also works vice-versa.
Actually I don't know right answer but I have a solution like below :
"&".htmlentities("Ú");
Your XSL transformation engine probably interprets your document as non-well-formed XML because of encoding issues. If that text containing Ú is stored using some 8-bit encoding (like ISO-8859 variants), then this character will not produce a valid UTF-8 octet if it is used as such without any character conversion. Invalid characters in an XML document will mean it is not well formed XML and processing it as XML is forbidden.
There are many points where that encoding error might happen:
it could be stored in the database incorrectly
it could be read from the database incorrectly
you might produce your XML by concatenating strings that use different encodings
you might manipulate the text with a tool or method that can't handle your encoding or is not aware of it
your XSLT engine might not be aware of the correct encoding of the input stream resulting a rejected file even though it has no encoding error
My random guesses for the probable causes of that are points 3 and 5.

Parse XML with special characters (UTF-8)

I'm starting out with some XML that looks like this (simplified):
<?xml version="1.0" encoding="UTF-8"?>
<alldata>
<data name="Forsetì" />
</alldata>
</xml>
But after I've parsed it with simplexml_load_string the special character (the i) becomes: ì which is obviously pretty mangled.
Is there a way to prevent this from happening?
I know for a fact the XML is fine, when saved as .txt and viewed in the browser the characters are fine. When I use simplexml_load_string on the XML and then save values as a text file, or to the database, its mangled.
This looks SimpleXML is creating a UTF-8 string, which is then rendered in ISO-8859-1 (latin-1) or something close like CP-1252.
When you save the result to a file and serve that file via a web server, the browser will use the encoding declared in the file.
Including in a web page
Since your web page encoding is not UTF-8, you need to convert the string to whatever encoding you are using, eg ISO-8859-1 (latin-1).
This is easily done with iconv():
$xmlout = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $xmlout);
Saving to database
You database column is not using UTF-8 collation, so you should use iconv to convert the string to the charset that your database uses.
Assuming your database collation is the same as the encoding that you render in, you will not have to do anything when reading from the database.
Explanation
In UTF-8, a 0xc2 prefix byte is used to access the top half of the "Latin-1 Supplement" block which includes characters such as accented letters, currency symbols, fractions, superscript 2 and 3, the copyright and registered trademark symbols, and the non-breaking space.
However in ISO-8859-1, the byte 0xC2 represents an Â. So when your UTF-8 string is misinterpreted as one of those, then you get  followed by some other nonsense character.
It's very likely that the XML is fine, but the character gets mangled when stored or output.
If you're outputting data on a HTML page: Make sure it's encoded in UTF-8 as well. If your HTML page is in ISO-8859-1, you can use utf8_decode as a quick fix; using UTF-8 is the better option in the long run.
If you're storing the data in a mySQL, you need to have UTF8 selected as the encoding all the way through: As the connection's encoding, in the table, and in the column(s) you insert the data into.
I've also had some problems with this, and it came from the PHP script encoding. Make sure it's set to UTF-8.
If it's still not good, try printing the variable using uft8_encode or utf8_decode.
XML is strict when it comes to entities, like & should be &amp; and ì should &igrave;
So you will need a translation table.
function xml_entity_decode($_string) {
// Set up XML translation table
$_xml=array();
$_xl8=get_html_translation_table(HTML_ENTITIES,ENT_COMPAT);
while (list($_key,)=each($_xl8))
$_xml['&#'.ord($_key).';']=$_key;
return strtr($_string,$_xml);
}
Late to the party... But I've faced this and solved like below.
You have declared encoding in XML so if you load xml file using DOMDocument it won't cause any issue.
But in case it happens in other use case, you can use html_entity_decode like below:
html_entity_decode($xml->saveXML());

Categories