Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: [PHP] Re: json_encode() behavior and the browser

27 views
Skip to first unread message

Christoph Boget

unread,
Sep 1, 2010, 9:49:30 AM9/1/10
to João Souza, php-g...@lists.php.net
> You should set the charset of your page by meta tag in its head.

Do you have a source of reference to which you point me?

thnx,
Christoph

João Cândido de Souza Neto

unread,
Sep 1, 2010, 9:51:44 AM9/1/10
to php-g...@lists.php.net
http://www.w3.org/TR/html4/charset.html

I hope it can help you.

PS: json_decode works only in utf8.

--
João Cândido de Souza Neto

"Christoph Boget" <cbo...@hotmail.com> escreveu na mensagem
news:AANLkTikPQDCkRQ7ctjwCC...@mail.gmail.com...

Christoph Boget

unread,
Sep 1, 2010, 10:00:14 AM9/1/10
to João Cândido de Souza Neto, php-g...@lists.php.net
> http://www.w3.org/TR/html4/charset.html
> I hope it can help you.
> PS: json_decode works only in utf8.

I understand charsets. I understand the difference between the
charsets. What I don't understand is how json_encode() is taking the
*exact same input* and behaving differently (breaking in one case,
working in another) depending on the browser being used. And taking
your statement that json_encode() works only in utf-8 as a given, how
can I guard against the different behaviors on the backend, where
json_encode() is getting executed. Should I do some kind of header
sniffing prior to every call to json_encode() and massage the data
accordingly depending on what I find? That seems somewhat excessive.
But based on what you are saying and based on what I'm witnessing, it
seems like there is no other way around that.

thnx,
Christoph

Christoph Boget

unread,
Sep 1, 2010, 9:48:24 AM9/1/10
to PHP General
> It can have something to do with your browser codification (UTF8,
> ISO-8859-???).

But why would that be the case? Is json_encode() actually encoding
the string differently in each case? And would it encode it
differently if I wrote a script to take the same input and ran it from
the command line?

Is there a way I can get around this on the back end? Make it so that
it'll behave the same in all cases?

thnx,
Christoph

João Souza

unread,
Sep 1, 2010, 9:42:04 AM9/1/10
to jcb...@yahoo.com, php-g...@lists.php.net
You should set the charset of your page by meta tag in its head.


2010/9/1 Christoph Boget <christo...@gmail.com>

--

João Cândido de Souza Neto

unread,
Sep 1, 2010, 10:07:06 AM9/1/10
to php-g...@lists.php.net
Are you setting the charset in your html head?

If not, its using the charset set in your browser, which can be different
from one to another.

In this case, you must set if via meta tag to avoid it.

--
João Cândido de Souza Neto

"Christoph Boget" <cbo...@hotmail.com> escreveu na mensagem

news:AANLkTimbfbgUnifhThZTP...@mail.gmail.com...

João Cândido de Souza Neto

unread,
Sep 1, 2010, 10:25:39 AM9/1/10
to php-g...@lists.php.net
In this case, you are right. It has nothing to do with the browser.

You´ll need a more detailed debug so you can see excatly what´s happening.

--
João Cândido de Souza Neto

"Christoph Boget" <cbo...@hotmail.com> escreveu na mensagem

news:AANLkTikcTht0nXz1hi0EzJ=i2M3716Wepd=DepP...@mail.gmail.com...
>> Sorry about the error:
>> In this case, you must set IT via meta tag to avoid it.
>
> Ok, let's try this using a different approach. Consider the following
> pseudo-code:
>
> <?php
> $result = mysql_query( 'SELECT name, date FROM table WHERE field =
> "value"' );
> $array = array();
> while( $row = mysql_fetch_assoc( $result ))
> {
> $array[] = $row;
> }
>
> $string = json_encode( $array );
> ?>
>
> Why does the charset of the browser matter one whit to the value of
> either $row['name'] or $row['date'] such that it would break
> json_encode() in one case and not the other. Is it that PHP is taking
> the string which is returned as part of the result set and encoding it
> to match the charset passed in from the browser?
>
> thnx,
> Christoph
>
> * Disclaimer : the actual code (and data repository) I am using is
> slightly different from the above but is similar enough so that it's a
> valid representation


João Cândido de Souza Neto

unread,
Sep 1, 2010, 10:10:06 AM9/1/10
to php-g...@lists.php.net
Sorry about the error:

In this case, you must set IT via meta tag to avoid it.

--
João Cândido de Souza Neto

""João Cândido de Souza Neto"" <jo...@consultorweb.cnt.br> escreveu na
mensagem news:16.27.0741...@pb1.pair.com...

Christoph Boget

unread,
Sep 1, 2010, 10:20:02 AM9/1/10
to João Cândido de Souza Neto, php-g...@lists.php.net
> Sorry about the error:
> In this case, you must set IT via meta tag to avoid it.

Ok, let's try this using a different approach. Consider the following

João Cândido de Souza Neto

unread,
Sep 1, 2010, 9:23:54 AM9/1/10
to php-g...@lists.php.net
It can have something to do with your browser codification (UTF8,
ISO-8859-???).

--

João Cândido de Souza Neto

"Christoph Boget" <cbo...@hotmail.com> escreveu na mensagem

news:AANLkTi=45-HEtO2MYhQ116GV6...@mail.gmail.com...
> I'm curious if the behavior of json_encode() is influenced by the
> browser at all. I have a page that returns search results. If I
> access the page and perform a search using Chrome, the following error
> shows up in the log:
>
> PHP Warning: json_encode() [<a
> href='function.json-encode'>function.json-encode</a>]: Invalid UTF-8
> sequence in argument in [PAGE] on line [LINE]
>
> If I access the page and perform a search, the exact same search using
> the exact same parameters, using Firefox then I get the expected
> results. When I var_dump() the return value of json_encode(), I see
> that it is a null in the case where I accessed using chrome but the
> expected string in the case where I accessed using firefox. In both
> cases, the input array is identical.
>
> Given the identical input and different output, the only thing I can
> figure is that the headers sent to the server as part of the request
> figure in to how json_encode() behaves. Is that the case? Or am I
> barking up the wrong tree?
>
> To be clear, I'm not talking about how the browser ultimately handles
> the json encoded data. I know there can be issues with that. I'm
> talking about the process before the data is even shipped to the
> browser -- about how json_encode() behaves when executed as part of
> the PHP script.
>
> thnx,
> Christoph


Christoph Boget

unread,
Sep 1, 2010, 9:17:53 AM9/1/10
to PHP General
0 new messages