What code to use related to PHP Encoding? - php

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'); ?>

Related

Special character PHP HTML

I have a problem of special character writing on my website coded in PHP (data from database and normal writing html)
Code :
code in Sublime text
Result :
result in web
I have in my header :
‹meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"›
It's important that your entire line code has the same charset to avoid issues where characters displays incorrectly.
There are quite a few settings that needs to be properly defined and I'd strongly recommend UTF-8, as this has most letters you would need (Scandinavian, Greek, Arabic).
Here's a little list of things that has to be set to a specific charset.
Headers
Setting the charset in both HTML and PHP headers to UTF-8
PHP: header('Content-Type: text/html; charset=utf-8');
(PHP headers has to be placed before any kind output (echo, whitespace, HTML))
HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
(HTML-headers are placed within the <head> / </head> tag)
File-encoding
It might also be needed for the file itself to be UTF-8 encoded. If you're using Notepad++ to write your code, this can be done in the "Format" drop-down on the taskbar. You should use UTF-8 w/o BOM (see this SO).
Other
Some specific functions have the attribute of a specific charset, and if you are using such functions, it should be specified there as well. Read the documentation for each function.
Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.

$_GET doesn't work correctly with Internet Explorer (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

I get a "�" on my page, PhpMyAdmin shows "é" not sure how to fix it

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.

Character encoding in javascript file and a simple php file

I got problem with some special characters. I have defined the meta tag as
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
so far so good. But in one occassion I have a select tag with many options with many special characters. I got problem showing (å ä ö). They are defined in .js file and are appended to the document using DOM. Pls. are there any solution except using &... forgot the name for that type.
Second character problem I have is in php. I try to send an email with Amazon SES.
<?php
$message['Body.Html.Data'] = "Please Confirm that you are a Tönt by clicking on this link"." ....<a href='s'>Tönt</a>";
$message['Body.Html.Charset'] = 'utf-8';
?>
the ö is not showing properly?
You'd better use:
<?php header('Content-type: text/html; charset=utf-8'); ?>
At the very top of your PHP file, no need to use http-equiv metas when you can send some real http headers ;-)
Also check if your files (both PHP and JS) are encoded in UTF8 in your editor.
Setting an UTF8 header on non UTF8 files will produce strange results for sure!

PHP print() Arabic string

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,

Categories