Danish letters MySQL output - php

I got some varchar fields in my MySQL database containing danish letters (æ, ø, å). When browsing the database with phpmyadmin, the letters appear correctly, however when I obtain the field through a query, and try to display the field, they are changed ("ø" becomes "ø"). I tried changing the collation to both latin1 and utf-8 (both danish versions), but without luck. I can't even figure out if it is the database or my code that is the issue. Anyone who has seen this before?
Edit: I'm adding the code to read and display the database content. The issue is confirmed in the "users_last_name" and "address_street", but is likely present all over (currently these are the only fields with danish letters).
Code:
<?php
// Query to load information on projects
$main_query = $this->db->query('SELECT project_id, project_name, project_image_src, project_owner FROM ed_projects');
foreach ($main_query->result() as $row) {
// Get adress of the current project in the "foreach" loop
$project_id = $row->project_id;
$address_query = $this->db->query("SELECT * FROM ed_project_address WHERE project_id='$project_id'");
$address_row = $address_query->row();
// Get the name of the user who owns the current project
$user_id = $row->project_owner;
$user_query = $this->db->query("SELECT users_first_name, users_last_name FROM ed_users WHERE id='$user_id'");
$user_row = $user_query->row();
?>
<div class="projectAvatar">
<?php if ($row->project_image_src) {
echo "<img src=".$row->project_image_src.">";
} else {
echo "NoImg";
}
?>
</div>
<div class="projectInformation">
<?php echo $row->project_name; ?> <br />
<?php echo $address_row->address_street." ".$address_row->address_number; ?> <br />
<?php echo $user_row->users_first_name." ".$user_row->users_last_name; ?> <br />
</div>
<?php
}

If things are working in phpMyAdmin, but not on your own web pages, it's likely to be a problem with the character encoding of your web pages. Assuming you're using an HTML5 doctype, try just adding:
<meta charset="utf-8">
to the <HEAD> section of your site.
Basically, from your comments and code, it seems like you're successfully storing your Danish characters using UTF-8 encoding in your database. (The table's collation setting won't affect that; collations determine sort orders and comparisons, but not the actual character set used for storage.) To make characters appear correctly on a web page, you need to tell the browser what character encoding you're using for your page; adding the <meta charset...> header does this. phpMyAdmin's web pages will almost certainly be using UTF-8 as their character set, so if that's working, you should change your pages to match it.
As I mentioned, I feel that by far the best full explanation of how this all works is given in Joel Spolsky's The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!), which will tell you about how this stuff should be done in all its gory detail.

try this ANSI (Windows-1252).
read from
Differences Between Character Sets

Related

Simplifying utf8_encode

So I'm trying to find a fast way to show all my results from my database, but I can't seem to figure out why I need to add the utf8_encode() function to all of my text in order to show all my characters properly.
For the record, my database information is both French and English, so I will need special characters including à, ç, è, é, ê, î, ö, ô, ù (and more).
My form's page has the following tag:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
My database, all my tables and all my fields are set to utf8_general_ci.
When I want to echo the database information onto the page, I use this query:
public function read_information()
{
global $db;
$query = "SELECT * FROM table WHERE id='1' LIMIT 1";
return $db->select($query);
}
and return the information like so:
$info = $query->read_information();
<?php foreach ( $info as $dbinfo ) { ?>
<pre><?php echo $dbinfo->column; ?></pre>
<?php } ?>
However, if I have French characters in my string, I need to <pre><?php echo utf8_encode($info->column); ?></pre>, and this is something I really want to avoid.
I have read up the documentation on PHP.net regarding utf8_encode/utf8_decode, htmlentities/html_entity_decode and quite a few more. However, I can't seem to figure out why I need to add a special function for every database result.
I have also tried using mysqli_query("SET NAMES 'utf8'", $mysqli); but this doesn't solve my problem. I guess what I'm looking for is some kind of shortcut where I don't have to create a function like make_this_french_friendly() type of thing.
Ensure all the stack you are working with is set to UTF8 from db, web server, page meta etc
checking things like
ini_set('default_charset', 'utf-8')
should output simple stuff then in my experience
As #deceze pointed out, this thread provided proper insight using $mysqli->set_charset('utf8');.
Maybe use UTF-8 without BOM encoding for your file?
header('Content-type: text/html; charset=utf-8');
... in PHP (you can also do it with "ini_set()" function) and:
<meta charset="utf-8">
... in HTML.
You have also to set the right encoding for you database tables.
Possible duplicate of "GET" method encoding French characters incorrectly in PHP
Maybe your text coding is not be UTF-8.
Please look: What's different between UTF-8 and UTF-8 without BOM?
Maybe it can helps you.

Wordpress foreign language in .php

I'm editing a site for someone, and they are using wordpress, which I really don't like, but hey, I didn't pick it. I need to change some text on their page to Portuguese characters such as Ç or Ã. I've read in a few places, that I need to change from ASCII to UTF-8, but I'm not sure where to do that, or how to do it across the whole site. Am I changing a database to UTF-8, or each individual php file? Hopefully somebody knows, thanks.
Thanks to the comments below, I have most of the site running correctly, but now I can't get the foreign characters in just certain spots, for example, anywhere I'm using code like this inside of a .php file.
$email_list = do_shortcode('[pl_modal title="Join our email list" label="<img class=\'\' title=\'Join our email list\' src=\'/wp-content/uploads/2013/02/email_icon.png\' /><br /><span>INSCREVA-SE A NOSSA<br />LISTA DE E-MAILS</span>"][gravityform id=1 title=false][/pl_modal]');
The portugese in the above code, if I add non english characters, I get a constantly loading error. More code, that does the same thing.
'<div class="graphicbuttons_cont">' .
'<a href="https://maps.google.com/maps?saddr={19}&daddr={20}" target="_blank">
<img title="Get Store Directions" src="/wp-content/uploads/2013/02/getdirection_icon.png" /><br /><span>LOCALIZACOES <br><br /> </span>
</a>' .
'</div>' .
the LOCALIZACOES in above text, should have special characters, but it won't hold them. I have changed everything to UTF8 that I can find. But there is nothing inside this specific file that says utf8, should I add something?
Alright, so, if you change everything to utf8, and on wordpress all of your html code is in php files, the way I've used to use special characters is this
thesauruslex.com/typo/eng/enghtml.htm
for example
<span>LOCALIZA&CcedilOES </span>
will output LOCALIZAÇOES
Thanks to everyone for the help, I guess I could have been clearer on the original question.
Everything in your application needs to be UTF-8.
Your MySQL string columns should be utf8_unicode_ci.
You need to ensure that your MySQL connection charset is set to UTF-8. You can do this via the query SET NAMES utf8 (run once after every connection) or you can modify your my.cnf file if you have access to it.
Your web pages should be served with <meta charset="utf-8">
You can check and validate what kind of input you're receiving by using the PHP function mb_check_encoding.
There's also a PHP ini setting called default-charset.
This can be changed two ways depending on your theme file. In the header.php file this should be near the top:
<meta charset="<?php bloginfo('charset'); ?>">
You use to be able to change this in the wordpress backend under settings -> reading. I believe now you have to manually change this in the wp-config.php file:
define('DB_CHARSET', 'utf8');

Data in MySQL database doesn't show correctly in website

I am trying to translate a English website to Persian. problems i was facing was :
website were loading in Latin Unicode, so I had to change the charset to utf-8 so contents show correctly in Persian
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
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);
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.

Getting rid of HTML entities in a web title generated in PHP

I have a website with the content management system GetSimple which is written in PHP. I edited it as I needed, however, in the header, this is what is supposed to be there:
<title><?php get_page_clean_title(); ?> - <?php get_site_name(); ?></title>
The problem is that I am Czech and I have to use special characters (á, é, í, ó, ú, ů, ě, š etc.) and if you opened my website and saw the source code, you would see this:
<title>Tomáš Janeček - osobní web - Tom**áš** Janeček | Personal Website</title>
Instead of "Tomáš Janeček - osobní web - Tom*áš* Janeček | Personal Website".
What is bothering me are those HTML entities, which are only in the second part of the title. á stands for "á" and š stands for "š".
I know it's supposed not to hurt SEO, but I'm doing this to keep the code clear.
Is there a way to decode it or just change the get_site_name() to some better function that would have no problems with these extra characters? I don't want the entities in my code.
I think that it's not this concrete .php file that should be edited to make it as I want it to be, however, I hope it could be solved somehow simply in this file.
The CMS includes tens of .php files and I'm not sure what should I search for. I've looked for some code with PHP entities in "suspicious" files but I found nothing that helped me.
If you need it, the whole CMS can be downloaded here
Thanks for your help in advance.
Edit1:// --------------------------------------------------------------------------------------
Of course I have this meta included.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
And no, I don't use any database. That will come with studying Joomla! :)
I want to emphasize that the title has 2 parts - get_page_clean_title() and get_site_name(), both of them include my whole name and only one displays it in the source code with HTML entities.
I have found the functions in another file:
The FIRST one is the one that doesn't put HTML entities into the source code - this is what I want from the second function lower.
function get_page_clean_title($echo=true) {
global $title;
$myVar = strip_tags(strip_decode($title));
if ($echo) {
echo $myVar;
} else {
return $myVar;
}
}
The SECOND function does what it is supposed to do, but it gives the output with HTML entities and that is the problem.
function get_site_name($echo=true) {
global $SITENAME;
$myVar = trim(stripslashes($SITENAME));
if ($echo) {
echo $myVar;
} else {
return $myVar;
}
}
Both of the functions above are in the same file.
I tried to replace the problematic function with the one working well with changing variables names to the right values, however, it stopped working at all :/
So, to conclude, the whole page is OK, there are no HTML entities except one place - the second half of the title with get_site_name function.
Furthermore, the problems is ONLY at the SOURCE CODE. The final displaying is okay.
Thanks for your replies so far, I'm glad for such fast and valuable replies. I really appreciate that.
I think you have a charset problem. If you want the special characters to display them in the right way, add
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
to your html/php file. Also check that your data is UTF-8 codified.
If you are getting your data from a MySQL database, check the columns use utf-8charset. Also set the charset for the connection with this query to ensure you are getting the data with the right codification.
set names utf8;
Tome, ensure that your *.php or database or whatever data is going off, is in UTF-8 and your meta charset on index is utf-8 also.
http://www.jakpsatweb.cz/cestina.html - Please visit this web for information about diacritics in html. You'll see the table of signs in each encoding.
How to save Russian characters in a UTF-8 encoded file

Hebrew fonts is not showing

I have an API in which i am showing some videos of english and hebrew. Its all running fine except the title written in Hebrew language is not showing instead of it its returning sign of "??"
Any idea ??
Make sure you are serving UTF-8 by adding <meta charset="UTF-8" /> in between your head tag.
Make sure the database content is UTF-8. If it's not UTF-8, you'll need to convert the database data from the encoding used in the database to UTF-8 before you push it to the web browsing client like this: $str = mb_convert_encoding($str, "UTF-8", "DATABASE-ENCODING-USED"); More info and examples: http://php.net/manual/en/function.mb-convert-encoding.php
One of both will be the solution to your the problem.
Update
Might be far-fetched, but make sure your browser is able to display a hebrew font type too.
In fact, you don't tell us how you check your database (with a client or via browser) and we don't know how you test the display.
To avoid any "hidden" errors in your debugging efforts, try this code:
<?php
/*
Make sure the web browser receives a header telling it there's UTF-8 inside.
*/
header('Content-Type: text/html; charset=UTF-8');
/*
Write a title that will display correctly
if there's no font-related issue in your browser.
*/
echo('<h1>זהו מבחן.</h1>');
/*
Now connect to database,
and use a simple SELECT to fetch something from the database.
Replace YOUR-MYSQL-USERNAME, YOUR-MYSQL-PASSWORD and YOUR-MYSQL-DATABASE
with your individual values...
*/
$dbh = mysql_connect("localhost","YOUR-MYSQL-USERNAME","YOUR-MYSQL-PASSWORD");
if(!$dbh)
{
exit('Could not connect: ' . mysql_error());
}
$result = mysql_query('SELECT * FROM YOUR-MYSQL-DATABASE LIMIT 1',$dbh);
mysql_close($dbh);
/*
Now dump it nicely to the screen and exit.
*/
echo('<pre>');
var_dump($result);
echo('</pre>');
exit();
?>
That will help you nailing down the problem.
If it does not show your Hebrew fonts nicely, it's most probably a font-and-browser issue.
In any other case, your database is not UTF-8 and you will need to convert using *mb_convert_encoding* or alike - as I explained above.
Have you encoded your HTML document to accept them?
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<html lang="he">
...
</html>
If the data is coming from the database, you want to make sure that you're retrieving it with the right encoding. If possible, you should run a SET NAMES query before the query (either SET NAMES 'utf8' or SET NAMES 'hebrew', depending on the collation in the DB).

Categories