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
Related
I am trying to write a function which could read an existing XML file and create a new one with all the data from the first one, but in a different encoding. As far I understand it, SimpleXML saves the file in UTF-8 encoding. My original XML file is Windows-1257.
Code:
public static function toUTF8()
{
$remote_file = "data/test/import/test.xml";
$xml = simplexml_load_file($remote_file);
$xml->asXml('data/test/import/utf8/test.xml');
echo var_dump('done');
exit;
}
This way the encoding of file is still not good. I wanted to try this:
$newXML = new SimpleXMLElement($xml);
But this takes only plain XML code as a parameter. How could I get the whole XML code from the object? Or how else could I create a new UTF-8 XML object and insert all the data from the old file?
I tried this out and saw problems importing the XML directly with SimpleXML. Despite the correct encoding declaration in the XML, it would output the wrong characters. So the alternative is to use a function like iconv which can do the conversion for you.
If you don't need to parse the XML, you can just do this directly:
<?php
$remote_file = "data/test/import/test.xml";
$new_file = "data/test/import/utf8/test.xml";
$baltic_xml = file_get_contents($remote_file);
$unicode_xml = iconv("CP1257", "UTF-8", $baltic_xml);
file_put_contents($new_file, $unicode_xml);
If you need to do stuff with the XML, it gets a little more complicated because you have to update the character set in the XML declaration.
<?php
$remote_file = "data/test/import/test.xml";
$new_file = "data/test/import/utf8/test.xml";
$baltic_xml = file_get_contents($remote_file);
$unicode_xml = iconv("CP1257", "UTF-8", $baltic_xml);
$unicode_xml = str_replace('encoding="CP1257"', 'encoding="UTF-8"', $unicode_xml);
$xml = new SimpleXMLElement($unicode_xml);
// do stuff with $xml
$xml->asXml($new_file);
I tested this out with the following file (saved as CP1257) and it worked fine:
<?xml version="1.0" encoding="CP1257"?>
<Root-Element>
<Test>Łų߯ĒČ</Test>
</Root-Element>
Unless I'm wrong, the SimpleXML extension will just use the same encoding all the way through. UTF-8 is the default if no encoding is given but, if the original document has encoding information such encoding will be used.
You can use DOMDocument as proxy:
$xml = simplexml_load_file(__DIR__ . '/test.xml');
$doc = dom_import_simplexml($xml)->ownerDocument;
$doc->encoding = 'UTF-8';
$xml->asXml('as-utf-8.xml');
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;
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 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.