Passing Hebrew encoded text by AJAX (windows 1255) - php

When making an AJAX call from the client, the server responds with Hebrew text. The PHP page that returns the result is in Windows-1255.
For some reason, the result encoding is not Windows-1255, and all I see is gibberish instead of Hebrew.
The result purposely contains HTML tags.
I've tried urlencoding, base64 encoding, nothing works.
Thanks!

Can't you change it so that the server return UTF-8 instead of 1255?
There's absolutely no reason these days to continue using ANSI.

all i needed to do is put this in the respone php file
header('Content-Type: text/html; charset=windows-1255');
10x everyone!!!

beware: AJAX works only with UTF8, so be sure to convert characters before sending them, otherwise you can get mangled characters in Javascript.

you should also not use the string from get data,
create a new srting...

What encoding is the web page containing the AJAX call/display?

Related

JavaScript to PHP - POST encoding issue

I'm attempting to send a string from client-side JavaScript to a back-end PHP script. The string contains special quotes like ’ and “.
When I look at the console in Chrome I can see that these are sent in the POST headers as they are. On the PHP side I then immediately json_encode() the $_POST array and send it back to see what its collected. The special characters now look like this \u2019. This is for testing please note I would normally sanitize all post data.
I wish to use UTF-8 but I'm not sure what I'm missing. My HTML includes:
<meta charset="utf-8">
My PHP server has UTF-8 set as its default charset.
If I start saving such data to the database I start ending up with strings like this: â for ’. However this is not a database issue the characters are already bad before going into the database. MySQL purely accentuates them.
Any ideas?
Update
I've noticed that if I return the string back to javascript without using json_encode() then it's in its original format with the special quotes (’ and “) still.
Have you tried:
utf8_decode()
On the server side for the variables you're passing? PHP is likely expecting iso-8859-1 rather than uft-8.
Turns out there was an issue both sides of the pond. The issue PHP side which this question regards was that the data was being sent to the back-end via a GET request (url encoded). I have changed this to a POST request.
This has allowed me to specify the UTF-8 charset when sending the headers for the POST request.

Fetch contents from a unicode URL

I would like to fetch the contents (HTML) from this URL: http://www.tvsporedi.si/spored.php?id=Vaš kanal. I've tried with File_Get_Contents and cURL.
No matter how I construct the URL in my code the page always returns a blank page (a page with header&menu but no content). I tried url encoding the id parameter, leaving the id as it is, without any luck. The only change I can make to the url (it seems) is encoding the space (with %20. Encoding the š does not work.
So I guess what I'm asking is why does PHP "eat" the š? The PHP file is saved with UTF-8 encoding ...
Try fetching:
http://www.tvsporedi.si/spored.php?id=Va%C5%A1%20kanal
This works for me in a browser, and I would guess with whatever method you use.
I used Firebug to see how the browser was encoding the request...
works for me with urlencode:
readfile('http://www.tvsporedi.si/spored.php?id=' . urlencode('Vaš kanal'));
demo

Corrupted characters when jQuery.AJAX sends to PHP (UTF-8 & ISO-8859 incompatibilities)

I have a javascript/PHP script that does the following:
Uses javascript to find text on a web-page.
Transmits the text using jQuery AJAX to a PHP page.
The PHP stores the text in a MySQL database.
The trouble is, when I look at what has been stored in the database, some non-ASCII characters are corrupted.
I have simplified the problem and printed out the character codes of each letter to investigate what is going on.
For example: send over a single character, the pound sterling symbol.
When I check in PHP, what is being received is characters 0xC2 followed by 0xA3
(capital A circumflex follwed by pound sterling).
Ie getting a spurious extra character  before the £).
I've looked at similar problems which suggested setting the jQuery.ajax contentType etc, but none of this made sense to me.
Thanks
Sounds like you're got mixed character sets. UTF-8, ISO-8859 there. PHP won't mangle the single pound character into two on its own, but the browser might if it's been told to expect iso-8859 but is sent UTF-8 instead. the  is a dead giveaway for that.
Basically, make sure you're using UTF-8 at all stages of processing (database, PHP, html) and usually things will work much better.
Finally got this to work.
The problem seems to be that the jQuery.ajax transmits data to the server using UTF-8 but the PHP expects iso-8859-1.
Solution: in PHP convert UTF-8 to ISO using the utf8_decode function, e.g.
$incomming = utf8_decode($_REQUEST('incomming'));
And when you send data back for the ajax return handler, use utf8_encode() to convert back to UTF-8.
Other things that seem to work include using the javascript escape() function on the data prior to transmission to the server and then un-escape the data in PHP with URLdecode().
Other things I tried but couldn't get to work:
I tried to make ajax transmit in iso-8859-1 so it would be compatible with the PHP: In the jquery.ajax settings: contentType: "application/x-www-form-urlencoded; charset=iso-8859-1".
Seemed to have no effect.
I tried to make PHP use UTF-8: header('Content-Type: text/html; charset=utf-8').
Again it didnt work.

Special characters escaping with JS and PHP

my application geting Text from a input field an post it over ajax to a php file for saving it to db.
var title = encodeURIComponent($('#title').val());
if I escape() the title it is all OK but i have Problems with "+" character. So i use the encodeURIComponent().
Now i habe a Problem with german special characters like "ö" "ä" "ü" they will be displayed like a crypdet something....
Have some an idea how can i solve this problem?
Thx
I suppose this has to do with encoding : your HTML page might be using UTF-8, and the special characters are encoded like this :
>>> encodeURIComponent('ö');
"%C3%B6"
When your PHP page receives this, it has to know it's UTF-8, and deal with it as UTF-8 -- which means that everything on the server-side has to work with UTF-8 :
PHP code must use functions that can work with multi-byte characters
The database (db, tables, columns, ...) must use UTF-8 for storing data
When generating HTML pages, you need to indicate it's UTF-8 too, ...
For instance, if you are using var_dump() on the PHP side to display what's been sent from the client, don't forget to indicate that the generated page is in UTF-8, with something like this :
header('Content-type: text/html; charset=UTF-8');
Else, the browser will use it's default charset -- which is not necessarily the right one, and possibly display garbage.
You might use escape("AbcÄüö") and you would get "Abc%C4%FC%F6"
In php you could then use urldecode($myValue) to get "AbcÄüö" again

show hebrew words in a php response

i am sending a JSON response through a php script which has some hebrew words.but when i run this script on browser it is showing '?' instead of hebrew characters..
FYI.. database is in hebrew_general_ci collation
any help would be appreciated..
thanks..
you may not included appropriate CSS file. also charset variable is another problem while using native languages. use Utf-8 if you are using a unicode font
like
also problem may occur, if the created eot font is not good. i am not sure Hebrew need such font.
You need to set the appropriate character set in your HTTP Content-Type header for the HTML page that will ultimately display the data contained in your JSON. Try adding this to the top of your php script:
header('Content-Type: text/html; charset="windows-1255"');
Use a content-type header, as others have suggested:
header('Content-Type: text/html; charset=YOUR_CHOSEN_CHARSET');
Try charset=utf8, or charset=iso-8859-8. There's a list of character sets over here.

Categories