jason_encode fails with special characters – PHP

  php

Q(Question):

I have a user that typed the following into an input field

set 30ยบ

This data was stored in the table using software other than php.

Now I am writing some new access code in php and while the data is retrieved from the table correctly, the ajax response returns a null value for that column/field (see photo look for "PRJCTADDR1")

I return a response to the client like so through jason_encode:
echo json_encode($res_array);

The client gets a null value, see attached photo (again look for "PRJCTADDR1".

Is there some standard routine to strip this and other characters from the data stream or otherwise escape these value?

What values should I be on the lookout for?

Attached Images
File Type: jpg degree mark there.jpg (42.5 KB, 150 views)
File Type: jpg degree mark null value.jpg (20.1 KB, 178 views)

A(Answer):

Just a followup, just to confirm this is a jason_encode problem. Here is the formatted value of the encode.

[{…,"PRJCTNAME":"Proposed Multi-Legged Water Tower","PRJCTADDR1":null,…..}]

A(Answer):

A partial answer is to use htmlentities(string) to encode any string to be included in the json response array. However now I need to decode the result on the client where I now get this answer:

"PRJCTADDR1":"with Legs offset 30º"

A(Answer):

you could try urlencode() in PHP and decodeURIComponent() in JS.

A(Answer):

Ah! This is occurring in the response to an ajax call in a simple data field. The "htmlentities(string)" works very well.

But the decodeURIComponent() sounds like a good idea.

Thanks for your response.

BTW, you steered me toward EventBug. Seems to be broken with FireFox 8. You got any alternate ideas to put in another great tool like that?

A(Answer):

But the decodeURIComponent() sounds like a good idea.

but it can’t unescape entities, only %-escapes

BTW, you steered me toward EventBug. Seems to be broken with FireFox 8. You got any alternate ideas to put in another great tool like that?

I know it’s a bother. currently I don’t have a replacement. I only can hope that it will be supported in FF 9+

A(Answer):

Ok thanks, I will work on that

LEAVE A COMMENT