PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > utf-8 in $_POST
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
utf-8 in $_POST

Réponse
 
LinkBack Outils de la discussion
Vieux 07/01/2008, 15h39   #1
Olav Mørkrid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut utf-8 in $_POST

hello

does php have any built-in functions to convert post data from
whatever format it arrives in to whatever format i wish?

example:

i use iso-8859-1 internally, and even specify
accept-charset=iso-8859-1 in my html, but some browsers (phones) send
utf-8 anyway.

do i have to manually check if CONTENT_TYPE says "utf-8" and then
convert the $_POST array elements one by one, or does php have any
built-in functions to ease this?
  Réponse avec citation
Vieux 07/01/2008, 15h56   #2
pobox@verysmall.org
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] utf-8 in $_POST

Olav Mørkrid wrote:
> hello
>
> does php have any built-in functions to convert post data from
> whatever format it arrives in to whatever format i wish?
>
> example:
>
> i use iso-8859-1 internally, and even specify
> accept-charset=iso-8859-1 in my html, but some browsers (phones) send
> utf-8 anyway.
>
> do i have to manually check if CONTENT_TYPE says "utf-8" and then
> convert the $_POST array elements one by one, or does php have any
> built-in functions to ease this?


That's interesting question.

Do you generate the pages, respectively do you specify something like this -

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

My experience is that this does not affect only the displayed
characters, but the way the form fields are transported.

But perhaps I am wrong,
Iv
  Réponse avec citation
Vieux 07/01/2008, 16h52   #3
mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] utf-8 in $_POST

> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
>
> My experience is that this does not affect only the displayed
> characters, but the way the form fields are transported.
>
> But perhaps I am wrong,
> Iv


This works for me as well.

Put in utf-8 and you should be good to go.

You don't -technically- need to even change your database fields or
anything; MySQL will take the utf-8 content and treat it like normal
binary content.

(However, if you want to modify the data in MySQL, then you might want
to make sure you have all the right character sets and stuff...)

I've had 100% success with just inserting and selecting info back and
forth from MySQL and displaying it on the page, with the proper <meta>
tag. Works like a charm in all languages attempted - English, Chinese
(both Simplified and Traditional), Russian, etc... it will look funky
if you view it directly in MySQL command line client or if you forget
the utf-8 tag though (unless your TERM + column and connection is set
properly)
  Réponse avec citation
Vieux 07/01/2008, 19h01   #4
Olav Mørkrid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] utf-8 in $_POST

i specify iso-8859-1 in both header and body:

<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1"/>
<form action="/" method="post" accept-charset="iso-8859-1">

if two different people post the norwegian phrase "Godt nytt år"
(happy new year), it may appear in the following variations:

[CONTENT_TYPE] => application/x-www-form-urlencoded;charset=iso-8859-1
$_POST["input"] = "Godt nytt år"

[CONTENT_TYPE] => application/x-www-form-urlencoded;charset=utf-8
$_POST["input"] = "Godt nytt år"

i was just wondering if php had some setting or function that would
make it auto-convert $_POST data into one specific encoding. otherwise
i seem forced to do something like this in the beginning of my php
script:

if(ereg("utf-8", $_SERVER["CONTENT_TYPE"])) {
foreach($_POST as $key => $value)
$_POST["key"] = convert_utf8_to_iso8859($value);
}
  Réponse avec citation
Vieux 08/01/2008, 01h25   #5
Larry Garfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] utf-8 in $_POST

I have to ask... WHY are you forcing ISO-8859-1? If anything, you should be
forcing UTF-8. Then you can send, receive, and store data in UTF-8 ad cover
most human languages without having to change character set.

On Monday 07 January 2008, Olav Mørkrid wrote:
> i specify iso-8859-1 in both header and body:
>
> <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1"/>
> <form action="/" method="post" accept-charset="iso-8859-1">
>
> if two different people post the norwegian phrase "Godt nytt år"
> (happy new year), it may appear in the following variations:
>
> [CONTENT_TYPE] => application/x-www-form-urlencoded;charset=iso-8859-1
> $_POST["input"] = "Godt nytt år"
>
> [CONTENT_TYPE] => application/x-www-form-urlencoded;charset=utf-8
> $_POST["input"] = "Godt nytt år"
>
> i was just wondering if php had some setting or function that would
> make it auto-convert $_POST data into one specific encoding. otherwise
> i seem forced to do something like this in the beginning of my php
> script:
>
> if(ereg("utf-8", $_SERVER["CONTENT_TYPE"])) {
> foreach($_POST as $key => $value)
> $_POST["key"] = convert_utf8_to_iso8859($value);
> }



--
Larry Garfield AIM: LOLG42
larry@garfieldtech.com ICQ: 6817012

"If nature has made any one thing less susceptible than all others of
exclusive property, it is the action of the thinking power called an idea,
which an individual may exclusively possess as long as he keeps it to
himself; but the moment it is divulged, it forces itself into the possession
of every one, and the receiver cannot dispossess himself of it." -- Thomas
Jefferson
  Réponse avec citation
Vieux 08/01/2008, 09h20   #6
pobox@verysmall.org
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] utf-8 in $_POST

Olav Mørkrid wrote:
> i specify iso-8859-1 in both header and body:
>
> <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1"/>
> <form action="/" method="post" accept-charset="iso-8859-1">
>
> if two different people post the norwegian phrase "Godt nytt år"
> (happy new year), it may appear in the following variations:
>
> [CONTENT_TYPE] => application/x-www-form-urlencoded;charset=iso-8859-1
> $_POST["input"] = "Godt nytt år"
>
> [CONTENT_TYPE] => application/x-www-form-urlencoded;charset=utf-8
> $_POST["input"] = "Godt nytt år"


Hm... What User Agents? Are there User Agents that do not follow the
instructions?

> i was just wondering if php had some setting or function that would
> make it auto-convert $_POST data into one specific encoding.


I have been banging my head over this some time ago. The main problem I
had at that time is that I could not find a reliable way to detect the
incoming encoding, which is the base of conversion.

If the User Agents are so unstable, you can't expect consistent error,
but you should be prepared for all kind of deviations.

> otherwise
> i seem forced to do something like this in the beginning of my php
> script:
>
> if(ereg("utf-8", $_SERVER["CONTENT_TYPE"])) {
> foreach($_POST as $key => $value)
> $_POST["key"] = convert_utf8_to_iso8859($value);
> }


As said above, you assume that the inconsistency will persist. And this
assumption is not stable, as it seems the User Agents do not follow the
instructions you sent to them.

I would agree with the others and advice you to switch to UTF-8 - this
is the only encoding, where you can handle multiple alphabets _without_
knowing which alphabet it is.

Iv
  Réponse avec citation
Vieux 08/01/2008, 09h57   #7
Arvids Godjuks
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] utf-8 in $_POST

To author:

You'r going the wrong way.
Make your page utf-8, all text on it. Set utf-8 encoding and treat all
incoming data as UTF-8. If some agent (definetly not some browser - they all
know UTF-8) doesn't understand that (that could be some "hacker" writing
it's own bot) - that's his problem. Then you will get all data incoming in
utf-8 and you can just add, modify and select it in and from database (don't
forget to set database and tables to UTF-8, that is default in MySQL 4.1 and
above).

If you have to deal with utf-8 data, you need the mb_string extension, see
manual.

  Réponse avec citation
Vieux 08/01/2008, 09h59   #8
Per Jessen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] utf-8 in $_POST

Olav Mørkrid wrote:

> i specify iso-8859-1 in both header and body:
>
> <meta http-equiv="Content-type" content="text/html;
> charset=iso-8859-1"/> <form action="/" method="post"
> accept-charset="iso-8859-1">


Have you checked 1) what the webserver sends in the header and 2) what
the browser actually uses? I'm pretty certain I've had issues where
the meta tags were fine, but the server overrode me settings in the
header.


/Per Jessen, Zürich
  Réponse avec citation
Vieux 08/01/2008, 18h31   #9
tedd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] utf-8 in $_POST

At 11:57 AM +0200 1/8/08, Arvids Godjuks wrote:
>To author:
>
>You'r going the wrong way.
>Make your page utf-8, all text on it. Set utf-8 encoding and treat all
>incoming data as UTF-8. If some agent (definetly not some browser - they all
>know UTF-8) doesn't understand that (that could be some "hacker" writing
>it's own bot) - that's his problem. Then you will get all data incoming in
>utf-8 and you can just add, modify and select it in and from database (don't
>forget to set database and tables to UTF-8, that is default in MySQL 4.1 and
>above).
>
>If you have to deal with utf-8 data, you need the mb_string extension, see
>manual.


Good advice.

Last night I read a chapter in the book "Core Web Application
Development with PHP and MYSQL" by Wandschneider who said basically
that -- a good read, btw.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
  Réponse avec citation
Vieux 08/01/2008, 18h39   #10
Daniel Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] utf-8 in $_POST

On Jan 8, 2008 1:31 PM, tedd <tedd.sperling@gmail.com> wrote:
> Last night I read a chapter in the book "Core Web Application
> Development with PHP and MYSQL" by Wandschneider who said basically
> that -- a good read, btw.


I'm guess it was his name which comprised the first chapter.

--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
  Réponse avec citation
Vieux 16/01/2008, 14h56   #11
Olav Mørkrid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] utf-8 in $_POST

the user agents in question are various mobile phones, which as you
might guess are premature technology and have their own ways with
things.

here is an example posting from a Samsung D600 which insists on
posting form data in UTF-8 even though i serve it ISO-8859-1 and it
claims to support all character sets.

[_POST] => Array
(
[message] => Norwegian characters: øá
)

[_SERVER] => Array
(
[HTTP_ACCEPT_CHARSET] => *
[CONTENT_TYPE] => application/x-www-form-urlencoded; charset=utf-8
[HTTP_USER_AGENT] => SAMSUNG-SGH-D600E/1.0
Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2.3.3.c.1.101
(GUI) MMP/2.0
)

i would consider switching to utf-8 if i knew how make the windows
version of emacs work fine with utf-8 text files (and still work with
iso-8859-1 files as well).

On 08/01/2008, Per Jessen <per@computer.org> wrote:
> Olav Mørkrid wrote:
>
> > i specify iso-8859-1 in both header and body:
> >
> > <meta http-equiv="Content-type" content="text/html;
> > charset=iso-8859-1"/> <form action="/" method="post"
> > accept-charset="iso-8859-1">

>
> Have you checked 1) what the webserver sends in the header and 2) what
> the browser actually uses? I'm pretty certain I've had issues where
> the meta tags were fine, but the server overrode me settings in the
> header.
>
>
> /Per Jessen, Zürich
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h25.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,21930 seconds with 19 queries