0

I am trying to translate a English website to Persian. problems i was facing was :

  1. website were loading in Latin Unicode, so I had to change the charset to utf-8 so contents show correctly in Persian
  2. data in MySQL database are not correctly shown in website probably cause of the Unicode problem

What I have done:

<?php ini_set('default_charset','utf-8'); header('Content-type: text/html; charset=utf-8'); ?>

by this , problem #1 fixed

but for problem number 2 i still facing the issue, although i have altered the tables to use utf 8 , but problem still persists. I gladly like to see how anyone can help me with this. function bbcode ($str) { //$str = htmlentities($str);

$token = array(
            "'\[b\](.*?)\[/b\]'is",                                  
            '/\[i\](.*?)\[\/i\]/is',                                
            '/\[u\](.*?)\[\/u\]/is',                                
            '/\[url\=(.*?)\](.*?)\[\/url\]/is',                         
            '/\[url\](.*?)\[\/url\]/is',    
            '/\[img\](.*?)\[\/img\]/is',                            
            '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',                    
            '/\[mail\](.*?)\[\/mail\]/is',                            
            '/\[font\=(.*?)\](.*?)\[\/font\]/is',                    
            '/\[size\=(.*?)\](.*?)\[\/size\]/is',                    
            '/\[color\=(.*?)\](.*?)\[\/color\]/is',   
            "':big_smile:'is",  
            "':cool:'is", 
            "':hmm:'is",
            "':lol:'is",  
            "':mad:'is",
            "':neutral:'is",
            "':roll:'is",
            "':sad:'is",
            "':smile:'is",
            "':tongue:'is",
            "':wink:'is",
            "':yikes:'is", 
            "':bull:'is", 
            '/\[item\=(.*?)\](.*?)\[\/item\]/is', 
            '/\[spell\=(.*?)\](.*?)\[\/spell\]/is', 
            "':warrior:'is",
            "':paladin:'is",
            "':hunter:'is",
            "':rogue:'is",
            "':priest:'is",
            "':dk:'is",
            "':shaman:'is",
            "':mage:'is",
            "':warlock:'is",
            "':druid:'is",
            "'\[ul\](.*?)\[/ul\]'is",
            "'\[ol\](.*?)\[/ol\]'is",
            "'\[li\](.*?)\[/li\]'is",
            );    

thanks alot in advance

  • 1
    Make sure your `character set` and `collations` of database and table set to `utf8_general` something like this – M Khalid Junaid Sep 05 '13 at 20:38
  • 1
    possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – deceze Sep 06 '13 at 00:49

2 Answers2

1

Sorry, my reply wasn't clear enough. I was almost sleep. The databases are empty, so I don't have to convert anything, but when I am inserting data into them, the data doesn't appear correctly. BTW, I'm not good with php or mysql; I am reading these articles and suggestions for hours and I'm just getting more confused. Can you just tell me where should I enter the code and what code,

$link = mysql_connect("localhost","UserName","Password") or die(mysql_error());
mysql_set_charset("utf8",$link);
mysql_select_db("DataBase Name") or die(mysql_error());

I guess the thing I found out from these articles is to add the mysql_set_charset("utf8",$link) part to the above code while the server tries to connect to db, but I have tried that and its not working. My website uses includes so thats like this:

include("../../config/config.php");
$connect = mysql_connect("$db_host", "$db_user", "$db_pass")or die(mysql_error());
mysql_set_charset("utf8",$link);
Jon Gjengset
  • 4,078
  • 3
  • 30
  • 43
  • Firstly, you'll want to change `$link` to `$connect` above. You'll also want to make sure that you have `` in your HTML as well, otherwise it might not work correctly. – Jon Gjengset Sep 06 '13 at 16:17
  • i know, here the function is calling another function ( the one that describes $db , in that function i have added these two line : mysql_query("SET NAMES 'utf8'"); mysql_query("SET CHARACTER SET utf8"); but the problem with Writing at database still exists , but it REads the database correctly – hamidreza khosroabadi Sep 07 '13 at 08:42
  • i also , i have added the requried meta tags to my Header ,and website is loading perfectly good in persian, but the problem is with the Scripts SAving and REading data from Database, scripts are even reading the datas i manualy enter the database perefctly okay , but they're saving datas to database as latin1 so when i enter something in persian in for example shoutbox module it saves the data to database as سÙ?اÙ? – hamidreza khosroabadi Sep 07 '13 at 08:49
  • Oh, it saves the data as HTML escaped? That's not just the character set being wrong. Do you have any calls to `htmlentities` in your code? – Jon Gjengset Sep 07 '13 at 09:00
  • ow ! yeah i have function page($value) { $page = mysql_real_escape_string(stripslashes(htmlentities($value))); return $page; } function strip($value) { $value = mysql_real_escape_string(stripslashes(htmlentities($value))); return $value; } – hamidreza khosroabadi Sep 07 '13 at 09:44
  • and also the ones i added to to question post Up there – hamidreza khosroabadi Sep 07 '13 at 09:48
  • Try removing the `htmlentities` call and use it when you are printing instead. Also note that for older versions of php, you should use `htmlentities($value, ENT_COMPAT, 'UTF-8')`. – Jon Gjengset Sep 07 '13 at 11:24
0

Assuming you've correctly converted the data in your tables to UTF-8 (just changing the character set is not enough), it sounds like you might be having problems with the connection not being set up as UTF-8. Have a look at SET NAMES, and more specifically this question.

If you're not sure you've converted your data to UTF-8, I'd have a look at this question as well as this Wordpress article and make sure you've followed the steps.

Community
  • 1
  • 1
Jon Gjengset
  • 4,078
  • 3
  • 30
  • 43