Hello I'm having problems parsing hebrew xml file.
I use file_get_contents to read the file, and when I display it I get weird charaters. I searched all over the internet and found many functions and none works.
Input:
<ROW>
<C0>1</C0>
<טבלה>טבלת ישובים</טבלה>
<סמל_ישוב>967</סמל_ישוב>
<שם_ישוב>אבו ג'ווייעד )שבט(</שם_ישוב>
<סמל_נפה>62</סמל_נפה>
<שם_נפה>באר שבע</שם_נפה>
<סמל_לשכה_מנא>62</סמל_לשכה_מנא>
<לשכה>באר שבע</לשכה>
<סמל_מועצה_איזורית>0</סמל_מועצה_איזורית>
<שם_מועצה> </שם_מועצה>
</ROW>
Output:
> ROWDATA>
> ROW>
> C0>1
<äìáè\>íéáåùé úìáè<äìáè>
<áåùé_ìîñ967
<áåùé_íù\>)èáù( ãòééåå'â åáà<áåùé_íù>
<äôð_ìîñ62
<äôð_íù\>òáù øàá<äôð_íù>
<àðî_äëùì_ìîñ62
<äëùì\>òáù øàá<äëùì>
<úéøåæéà_äöòåî_ìîñ0
<äöòåî_íù\> <äöòåî_íù>
\> ROW>
The code I'm using is:
$xml = file_get_contents('hebrew.xml');
echo hebrevc($xml);
I tried with outputing header before:
header('Content-Type: text/html; charset=utf-8');
I also tried with utf8_encode and I get all the the time wrong charachers.
Please help me out :/
Try this:
$xml = file_get_contents('hebrew.xml');
mb_convert_encoding($xml, 'UTF-16LE', 'UTF-8');
echo $xml;
Related
I'm trying to use simplexml_load_string to get the Status and Text values from the following XML response;
<?xml version="1.0" encoding="utf-16"?>
<cXML payloadID="online" xml:lang="en" timestamp="2017-12-04T15:57:47.6693296+00:00">
<Response>
<Status code="402" text="
product 325552not in customer[20690] pricelist" />
</Response>
</cXML>
In my PHP code I am getting the XML above from $reply:
$reply = curl_exec($curl);
I am then using simplexml_load_string like so:
$responseData = simplexml_load_string($reply);
echo 'Sync Order - '. $order->getIncrementId() . ' Status '. $responseData->Response->Status['code'] .' - '. $responseData->Response->Status['text'];
But this doesn't seem to get the code and text from the XML response above. Wondering if anyone has any ideas to help?
Thank you.
Note: The cXML is correct.
I've tried it with the added xml header and UTF-16 encoding bit and it fails to load with an error...
PHP Warning: simplexml_load_string(): Entity: line 1: parser error :
Document labelled UTF-16 but has UTF-8 content in
A simple but crude way round it is to change the UTF in the xml element to UTF8...
$reply = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $reply);
$responseData = simplexml_load_string($reply);
This then gives the output as expected...
Status 402 -
product 325552not in customer[20690] pricelist
I am trying to develop my own XML RSS feed based on PHP output from MySQL queries. However I keep getting "entity X not defined" error messages for all the ASCII characters in my DB content fields, even though I have set everything to UTF8 encoding and charset (database connection, xml version, utf8_encode), but nothing removes the error:
<?php
$connection = mysqli_connect( .... )
$connection->set_charset("utf8");
header("Content-type: text/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<rss version="2.0">';
$query = mysqli_query($connection,"SELECT * FROM news ORDER BY pubdate DESC LIMIT 10");
while($row = mysqli_fetch_assoc($query)){
$title = utf8_encode($row['title']);
$content = utf8_encode($row['content']);
echo '<item><title>'.$title.'</title>';
echo '<description>'.$content.'</description></item>';
} // end while
echo '</channel>';
echo '</rss>';
?>
What am I missing?
Thanks a lot!
I also faced the same issue and fixed using below link
I think you are looking for this
http://help.simplytestable.com/errors/html-validation/general-entity-x-not-defined-and-no-default-entity/
You will have to escape the $title and $content variables. Check htmlspecialchars().
For a better solution, use DOM to create the the XML. This will make sure that you create a valid XML.
I am getting a a request like this and the url looks like this : http://domain.com/page.php?text={Arabic Word}
Now am trying to get the text using $_GET['text'] but i keep getting it like "????????" , whats the problem
<?php
header('Content-type: text/html; charset=UTF-8');
include('EnTransliteration.class.php');
$tr = new EnTransliteration();
$str = iconv( "utf-8//TRANSLIT//IGNORE","windows-1256", $_GET['text']);
$en_str = $tr->ar2en($str);
$string = <<<XML
<root>
<translation>$en_str</translation>
</root>
XML;
$xml = new SimpleXMLElement($string);
header('Content-type: text/xml; charset=UTF-8');
echo $xml->asXML();
?>
Your Apache server probably doesn't accept UTF-8 URL encoding. See this answer to solve your problem.
Hope it will help :)
Your HTML form needs to explicitly declare that the data must be sent as UTF-8. Otherwise, it will use the user's locale, which may not mean windows-1256, and certainly doesn't mean UTF-8.
e.g.
<form action="" method="get" accept-charset="UTF-8">
maybe this can help :
<?php
$title = 'أبجد هوز';
$path1 = '/product/'.rawurlencode($title);
echo $path1."<br>";
$path2 = rawurldecode($path1);
echo $path2;
?>
1-open start
2-open notepad
3-write your code
4-when you saving file make Encoding: utf-8
5-file name name.html name.php
I have two XML documents, both formatted like this:
<?xml version="1.0" ?>
<article>
<body>
<![CDATA[
*some text*
]]>
</body>
</article>
and I want to echo them using this:
<?php
$xml = simplexml_load_file("." . $filename);
echo $xml->body;
?>
But one of them works, the other just echos nothing. What is going on?
UPDATE:
The document which produces the error contains this appostrophe: '
When this apostrophe is removed, the code works. I need some way of escaping characters like this, how can I do it?
Just echo asXML() you may see your error with the second file.
echo $xml->asXML();
Here is a simple tutorial on SimpleXML: http://php.net/manual/en/simplexml.examples-basic.php
Espace your appostrophe:
<?php
$text = file_get_contents("." . $filename);
$text = str_replace("'", "'", $text);
$xml = simplexml_load_string($text);
echo $xml->body;
?>
Also, someone had a similar problem (no crash but garbage characters) and came up with the same solution. A bit later in that forum thread they speculate on utf8_encode and utf8_decode, which you could also try. Link: http://board.phpbuilder.com/showthread.php?10359181-RESOLVED-SimpleXML-apostrophe-problem&p=10886946&viewfull=1#post10886946
I have a slight problem. I need to parse a file and populate a web banner with the results. Problem is, the file is called : "_banner_products.php" and it's contents are as follows:
<?php header('Content-Type:text/xml'); ?><?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<carouselle>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
<firstLayer>
.....
.....
</firstLayer>
</carouselle>
How can I loop through this file to group all the "firstLayer" children into one and so on..
If I just use:
$file = fopen("_banner_products.php", "r");
while (!feof($file)){
$line = fgets($file);
}
simplexml_load_file throws this-
"_banner_products.php:1: parser error : Start tag expected, '<' "
Then I only get the contents of the <...> tags meaning there is no way for me to differentiate if I am out of the scope already.
Thanks for anyone responding. If anything is unclear I´ll try to explain more.
EDIT.
Thank you for the solution, indeed using the full URL worked:
simplexml_load_file("http://localhost/MySite/_banner_products.php");
You are having issue because simplexml_load_file is treating your file like a local xml file .. what you need to do is add the full URL
Example
simplexml_load_file("http://localhost/web/_banner_products.php");
Use Case getting layerName for example
_banner_products.php
<?php
header ( 'Content-Type:text/xml' );
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<carouselle>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
</carouselle>
view details
$xml = simplexml_load_file("http://localhost/lab/stockoverflow/_banner_products.php");
echo "<pre>" ;
foreach($xml as $key => $element)
{
echo $element->layerName , PHP_EOL ;
}
The most obvious way to do this is to strip out the first line, and add the XML declaration back in with your code.
You could also parse the file with PHP, using eval(), but be very sure about what you are parsing, as this could be a very large security hole.