tags:

views:

129

answers:

3

How can i know the character set in $_REQUEST ? and how to set the character set of $_REQUEST ?

+2  A: 

use mb_internal_encoding to know which encoding is currently set. If you application use a log of different encoding you have better to use mb_strlen.

Cheers

RageZ
thanks
lovespring
A: 

Usually you have control of the character encoding since you create the $_REQUEST from the HTML you send to the client.

ie: It is generated by a page you sent from PHP.

Thus you shouldn't have to detect the encoding.

Using the mb_functions requires enabling the multibyte extension - so if you're distributing code, you have to be aware not everyone will have it.

header('Content-Type: text/html; charset=UTF-8');

OR in HTML:

<meta charset="utf-8">

http://www.w3.org/International/O-charset

Edit: PHP6 has utf-8 support, not PHP5.

bucabay
“the string functions are UTF-8 compatible”. is it means strlen in php5 will return correct length of utf8 string?
lovespring
"As of PHP5, the string functions are UTF-8 compatible." That's definitively not correct. PHP's internal string functions all operate on the byte-level which means they cannot deal with multi-byte characters (valid for PHP < 6). All internal functions where the length of a character (in bytes) is important to its operation are error-prone when working with those character sets. The mentioned multibyte-extension (mb_*) has an overloading-ability but I'd strongly advise against using it.
Stefan Gehrig
I was way off there, PHP5 does not have any multibyte support.
bucabay
thanks for your reply, bucabay.
lovespring
+3  A: 

To make it short: you do not really know about the encoding (character set) used on the variables that are passed to your PHP script via GET or POST (especially GET is a problem here). By convention browsers POST forms to the server-side resource specified in the action-attribute using the page encoding which can be specified via an http-equiv-meta-tag (charset-meta-tag in HTML5) or via an HTTP header. Alternatively some browsers also respect the accept-charset-attribute on the form when chosing the correct encoding.

The encoding of GET parameters and the URL itself depends on the browser stettings and can therefore be controlled by the user. You should not rely on a specific encoding.

Generally you'll circumnavigate most encoding-related problems by consistently using UTF-8 for everything and by specifying the correct encoding in the HTTP-header (Content-Type: text/html; charset=UTF-8) - this will yield the correct encoding (UTF-8) in all the variables that are passed into your string (we're not talking about rouge scripts that deliberately try to mess with the encoding to allow for some attack vectors into your script). You also should not rely on non-ascii-characters in your GET parameters or in the URL (that's also a reason why SEO-friendly links remove those characters or substitute them).

If you made sure that UTF-8 is the only allowed character-set you can use mb_strlen($string, 'UTF-8') to check the length of a variable for example.

EDIT: (added some links)

Some things for you to read:

Stefan Gehrig
The encoding of the URL depends on the referring document too. Only if the URL is entered directly in the location bar the browser’s settings are crucial.
Gumbo
+1: Nice answer, very informative.
EvilChookie
Thanks @Gumbo for the clarification - that's what I actually meant.
Stefan Gehrig