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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dc2698
Forum Newbie
Posts: 7
Joined: Fri Jan 05, 2007 1:53 am

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

Post 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!
dml
Forum Contributor
Posts: 133
Joined: Sat Jan 26, 2008 2:20 pm

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

Post 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);
 
 
Post Reply