Friday 19 October 2012

How to read special character in php

Some time you store data in different language like French, Spanish etc.When you fetch this data on your page then the special character are not shown as they save in data base.They shows on page as question mark etc,this is problem is utf encode. If you are using xampp version of 1.7.3 and above then there is no problem because when you are fetching data from database then it's return encoded data then data shows in right way.But when your special character are not shown. So don't worry and no need to install latest xampp/wampp.Following are the special characters and method how to show this in our page.

a) Umlauts and accents:- Â Ã Ä Æ Ç È É Ñ Õ Ö Ø Ù Ú Û Ü ß ã ñ ò ó ô œ õû ü ÿ
b) Punctuation:- ¿ ¡ « » § ¶ † ‡ • - – —
c) Commercial symbols:- ™ © ® ¢ € ¥ £ ¤
d) Greek characters:- a ß ? d e ? ? ? ? µ ? p ? s t ? f ? ?G ? T ? ? ? S F ? O
e) Math characters:- ? ? ? v - ± 8 ˜ ? = ? = = × · ÷ ? ? ? ? ? ? ? ? ?

For that there is following ways for showing special characters.

1. utf8_encode()
2. header('Content-Type: text/html; charset=utf-8');


1. utf8_encode():- This is used with variable, for example if there is a variable
$userPassword = "#ÛÜõû!";
if these special character not print in your page then you need to used this function as following.
echo utf8_encode($userPassword);
Now it's print #ÛÜõû! on your page.


2. charset=utf-8:- Another way is just place this code on your page at the top once time header('Content-Type: text/html; charset=utf-8'); , then your special character shows on your page.
header('Content-Type: text/html; charset=utf-8');
But you can use utf8_encode() it's more predictable and accurate.

you can use both of this but I suggest you you can use utf8_encode() and it's solve your problem. Use this with variable where you wants to echo.

No comments:

Post a Comment