Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Php special character problem when posting

Status
Not open for further replies.

monkey64

Technical User
Apr 27, 2008
69
GB
This is driving me crazy:

I have a simple form and I'm trying to post back a single character and test it's ASCII code. Although it works for standard characters, special characters such as Ø give an incorrect code. The code should be ASCII 216. I'm getting ASCII 239.

I tried changing my character set from ISO-8859-1 to UTF-8, but it makes no difference. I should point out that I am on an "https" secure server. Works okay on a standard domain. What's happening?

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>

<?php

$a = $_POST['t'];
echo "The Character posted is: " . $a . "\r\nThe Character code is: " . ord($a) . "\r\nI am expecting ASCII Code 216";
?>

<form method="post" action="">

<input type="text" name="t" value="Ø">
<input type="submit" value="submit">
</form>

</body>
</html>

Any ideas?
 
i'm surprised you are getting anything at all. that doesn't look like a character supported by ASCII (
it looks more like unicode 00D8.
ord() won't work (so far as I know) on non-ascii character sets.

there are several equivalent functions for getting the character code of unicode characters.
 
Actually I solved the problem.
Needed to create a .htaccess file to set a default charset.

Code:
AddDefaultCharset ISO-8859-1

Working perfectly.
 
i am hopeless on foreign characters. more to the point php is also hopeless... and will be until version 6 is mainstream.

but nevertheless...

when i copy your character into this script I get the following

Code:
<?php
header('Content-Type: text/html; charset=UTF-8');
$string = 'Ø';
echo ord($string);
echo '<hr/>';
echo chr(216);
echo '<hr/>';
echo chr(239);
?>

Code:
175
?
?

which is interesting. the underlying codeset of the document is UTF-8 of course.

if i change the charset of the html output to ISO-8859-1 I get the following

Code:
175
Ø
ï

which is as it should be save that your character is being reported as asc 175 and not 216 or 239!


now to serve even more confusion. i changed my code to this

Code:
<?php
header('Content-Type: text/html; charset=ISO-8859-1');
echo <<<FORM
<form method="post" action="{$_SERVER['PHP_SELF']}">
<input name="char" type="text" /><input type="submit" />
</form>
FORM;

if (isset($_POST['char'])){
	echo "Character is $_POST[char] and asc value is " . ord($_POST['char']);
}
?>

and copied and pasted the character that you posted above

this was the result
Code:
Character is Ø and asc value is 216

and when i change the header to point back to UTF-8, i get character code 195 back.

so the answer is that (i assume) that each different encoding returns a different character code for the same perceived character.

all this really goes to show is that i wish i knew more about character sets!
 
All this really goes to show is that i wish i knew more about character sets!

You and me both!
Thanks for your help.
 
you don't need to do the .htaccess route. setting the header is fine (as per the tests above).

and if you want to standardise your tests on ISO-8859-1 then do an iconv before testing.

good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top