Page 1 of 1

My utf-form has problem..........

Posted: Sat Aug 30, 2008 5:04 am
by dc2698
My simple utf-form is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>A UTF-8 form</title>
</head>
<body>
<form name="utf-form" method="post" action="temp2.php">
<INPUT TYPE="text" NAME="textbox1" value="<?php echo trim(htmlentities(stripslashes($_POST['textbox1']))); ?>">
</form>
</body>
</html>

After I submitted with something like "天主教", this form returned with the textbox1 value as garbage characters, which is incorrect.

If I just use $_POST['textbox1'] without stripslashes() and htmlentities() and trim() then this form returned with the textbox1 value as "天主教", which is correct.

I need to use stripslashes() and htmlentities() and trim() so how shall I overcome the limit or solve this utf-8 PHP problem please?

Thanks for any replies in advance!

Re: My utf-form has problem..........

Posted: Sat Aug 30, 2008 7:46 am
by dml
It looks like you can get htmlentities not to break your string by specifying the charset as an argument. I'm not sure if any similar adjustment is required for stripslashes.

Code: Select all

 
<?php
header("Content-Type: text/html; charset=utf8");
$s = "???";
$ss = stripslashes($s);
$wrong = htmlentities($s);
$right = htmlentities($s, ENT_QUOTES, "utf-8");
assert($right===$s);
var_dump($s, $wrong, $right, $ss);