When I try and execute this code to print out an Arabic string: print("إضافة"); I get this output: إضاÙØ©. If I utf8_decode() it I'll get ?????. I have "AddLanguage ar" in my apache configuration but it doesn't help. How do i print out this Arabic string?
Also set your page language to utf8 eg:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and then see it if worked. If that still doesn't work, go and check this out, it is complete solution for the arabic language using PHP:
http://www.ar-php.org/en_index_php_arabic.html
You may want to check out this too:
http://www.phpclasses.org/browse/package/2875.html
It might be necessary to indicate to the browser which charset you are using -- I'm guessing it's UTF-8.
IN order to achive that, you might try putting this portion of code at the beginning of your script, before any output is generated :
header('Content-type: text/html; charset=UTF-8');
[utf8_decode][1] will try to decode your string from UTF-8 to latin1, which is not suited for Arabic characters -- hence the '?' characters.
You may want to set
default_charset = "utf-8"
in your php.ini. Default charset directive instructs the server to produce correct content type header.
You can also do it in runtime:
ini_set('default_charset', 'utf-8');
You may also want to check your browser font if it has Arabic support. Stick to common fonts like Arial Unicode and Times New Roman.
Well,
First: Add by the beginning of HTML page
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
second:
if you are using AJAX encode data using encodeURIComponent
Third:
First line of your PHP page should be
header('Content-Type: text/html; charset=utf-8');
and decode the data sent to PHP using urldecode
Regards,
Related
I work on a website that has different language interfaces, so far I use english and german.
when the german text is loaded, it shows weird characters like the following screenshot
though I use
header('Content-type: text/html; charset=utf-8');
and also in the html header
<META http-equiv="content-type" content="text/html; charset=utf-8">
what else can I do to solve it ?
Thanks
The content of the page needs to also be in UTF-8. Your content was probably made using MS Word, which uses Windows 1251 encoding. You need to re-save your document as UTF-8.
UTF-8 does not convert formats for you.
If those strings are saved in a file, the file has to be encoded in UTF-8 too.
If you're getting them from a database, they'll have to be stored as UTF-8 and you'll have to set the connection charset to utf-8.
You could also check whether your text is UTF-8 and if not, convert it with utf8_encode.
How do I interpret some characters into their proper form in PHP?
For example, the string is \u00c9rwin but PHP print it as Érwin, and the correct form must be Érwin
What is the proper PHP code for this? I am pretty sure this is not an HTML entity, or is it?
P.S. no encoding was declared on the PHP file
Look into utf8_encode and utf8_decode.
It's important as well to go UTF8 across the whole stack. What that means is that your database connection should be using UTF8 (here's how in MySQL), your HTTP Content-Type should be returning UTF8 (see mgraph's example below) and you should also be setting it in the meta tag so that there is no need to encode/decode at all as you're using the same charset everywhere.
add this in header:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
or:
<?php header ('Content-type: text/html; charset=utf-8'); ?>
I am trying to GET values from url
and I have ended up with a problem in IE
but all other browsers it works great.
This is my issue:
If text is some UTF-8 text as example:
$x=$_GET['txt'];
echo $x;
I got
???????
only in IE
still same problem and this is my all code
<?php
header('Content-Type: text/html; charset=utf-8');
$x=$_GET['id'];
echo $x;
?>
try with this word in id
سسسسسسس
You can put this meta tag inside the <head> if it's a charset issue (as an alternative to using header inside PHP):
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
UPDATE
If you're not encoding the value of x from the URL you should do something like:
Link
Using your sample text (سسسسسسس), once that's encoded using urlencode it should like this:
%D8%B3%D8%B3%D8%B3%D8%B3%D8%B3%D8%B3%D8%B3
I got it working by adding a charset meta tag and doing a simple urldecode:
echo urldecode($_GET['x']);
See screenshot on IE:
Try setting your page so the browser will recognize its encoding correctly. Mostly sending a proper header is enough:
header('Content-Type: text/html; charset=utf-8');
This is for UTF8 but you can send any encoding you want.
As you already stated yourself this could depending on the browsers character encoding settings. Try the utf8-function in PHP like
http://www.php.net/manual/de/function.utf8-decode.php
and
http://www.php.net/manual/de/function.utf8-encode.php
(:
Also look here:
Handling unicode values in GET parameters with PHP
try adding: urlencode & urldecode around your $x
in PhpMyAdmin it shows up as 'Petite-Réserve" but when i echo it to a webpage it shows as "Petite-R�serve" MyISAM latin1_swedish_ci is the database encoding and <!DOCTYPE html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /> is at the top of the page. Not sure how to fix this. I'm allowing users to input text and users are French and English. I'm using Google Chrome and it shows up as a question mark in a triangle. Any ideas?
You need to use the right content-type on the page - since you are outputting latin1 (as defined in your database), try this:
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
iso-8859-1 is the encoding name for latin1.
If you use a different encoding to output data to that is has been saved in, you have to encode or decode the data beforehand. Try
utf8_encode($value);
in this case.
Alternatively, change the encoding of your HTML to iso-8859-1.
Often best to decide on one charset and not to mix them. If you mix them you will have to convert between them. mbstring can help there. Best to switch your database to UTF-8. It is more flexible then the latin variations.
Check the HTTP response headers sent by your webserver. One of the headers might include the content-type, and that value in your http headers might not match the UTF-8 encoding declaration you've got in HTML.
I have a PHP file with one simple echo function:
echo 'アクセスは撥ねりません。';
but when I access that page i get this:
????????????
Can someone help me?
I also have my page encoding set to UTF-8, and I know it, because all of the browsers i used said so.
I also do this before the echo function:
mb_internal_encoding('UTF-8');
What does this do?
Does it help me?
All I need is to be able to echo a static Japanese string.
Thanks!
There are a few places where this could go wrong.
Firstly, if you aren't setting the output encoding in php with header()
header('Content-type: text/html; charset=utf-8');
or in your html with a meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
you will need to check the php.ini setting default_charset. Chances are this is defaulted to iso-8859-1
Secondly, you may also need to check the content encoding you are saving the php script as. If you are saving it as ASCII or some other latin charset, it will munge the characters.
I got it.
I just had to set the mbstring extension settings to handle internal strings in UTF-8. Thas extension is standard with my build of PHP 5.3.0.
Maybe you are printing Japanese characters contained in UTF-16 (extended set of chars)?
I just did a quick test and your example works for me, so it's most likely one of these:
Your file is not saved in UTF-8, but some other encoding, such as Shift-JIS. A decent editor should be able to let you see what encoding it used
Your server is sending bad http headers. Can you use some tool to check the headers and paste the results? Or the results you got from the browser?
The browser is using an incompatible font
I saved a file in UTF-8, pasted your code into it, and my server is serving the file with Content-Type: text/html; charset=utf-8 and it shows up just fine. Did not need to use the mb_ function or anything else.