0

I've a problem with storing my data into an XML file:

I am using a PHP script that receives data from user via mobile app and inserts it into an XML file. But if the user inserts arabic words (name-password-...etc), it's stored as question marks in the XML file.

Hint: I already set the XML encoding = UTF8.

Here's my code :

<html>
<head></head>
<form action="sign.php" method="post">
 name :<input type="text" name="name"/><br />
 pass :<input type="text" name="pass"/><br />
 mob :<input type="text" name="mobile"/><br />
 mail :<input type="text" name="email"/><br />
<input type="submit" name="send" value="send"/>
</form>


</html>
 <?php
header('Content-Type: text/html; charset=Windows-1256');
$test = utf8_encode("ياسمين");
echo mb_detect_encoding($test);

echo $test;
if(isset($_POST['send'])){



if(isset($_POST['name']) && isset($_POST['pass']) && isset($_POST['mobile']) &&isset($_POST['email'])) { 
 $name =  $_POST['name'];
     $pass = $_POST['pass'];
 $mob = $_POST['mobile'];
 $mail = $_POST['email'];

  $xml = simplexml_load_file('mowasla.xml');
$user = $xml->users->addChild('user');
$user->addChild('user_name', $name);
$user->addChild('pass', $pass);
$user->addChild('phone', $mob);
$user->addChild('email', $mail);
file_put_contents('mowasla.xml', $xml->asXML());
echo("done");
echo ($name);


}else{
 echo("error");
 die;

     }
  }

 ?>
Yasmine Ra'fat
  • 97
  • 1
  • 1
  • 5

1 Answers1

0

In PHP most of the XML related functions expect the string-data you pass in there to be UTF-8 encoded.

In your case the arabic text is not UTF-8 encoded. Therefore the XML library does replace the binary character sequences it can not identify with a substitution character, here the question mark (?).

So before you can insert the arabic words, you need to ensure the strings are UTF-8 encoded.

Please see how this generally works (this is first of all not related to PHP but to HTTP and HTML) on the W3C website:

To technically answer your question with a solution to your problem you at least need to share in which encoding the submitted data (the arabic words, username, password) is. Without that information displaying question marks looks correct because like your computer, we can not say either.

See as well:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836