I have a little php program which send emails to customers.
The email containes text which is written as html code and also contains data from my MySQL database.
I have tried to change the encoding for the email as UTF-8 and ISO-8859-1.
Depending on the encoding either the html text or the database data is displayed wrong when a word includes special characters.
How do I need to change setting so my emails are displayed correct.
Your question is very limited. I guess your using the code below in your html.
<meta charset="...">
I also guess that your using mysqli to retrieve the data from the database and you not using
mysqli_set_charset()
Here is the Docs for the function above.
I am sorry I did not describe my problem clear enough.
I wanted to send emails through a php file. This email would contain text which is included in the file but also data from a database.
In my php file header I am using
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
In the email header $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
The emails then contains strange symbols for all special letters for parts which are taken from the database.
I have found the following solution which helped me to solve the problem. Just after the database connection it is required to enter the following few lines.
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
Related
I have mysqldb with some in on series episodes.
But when i fetch this and echo it i get some strange square with a question mark in it.
I want to replace all these to either nothing or something more suitable.
I have googled my ass of and found solution for double quotes but i would like a function that matches all forms of quotes
here is some example
“senior tanning”
�senior tanning�
Mayor’s
Mayor�s
don't know the names of these in English that is why i posting examples :)
I started with php first time yesterday so please be nice :)
When you echo the contents from the database, make sure you have the charset meta tag set to utf-8.
HTML DOCUMENT
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
And in PHP before any output:
header('Content-Type: text/html; charset=utf-8');
And lastly set the MySQL transfer charset:
PDO:
$db->query("SET NAMES utf8");
Or oldschool:
mysql_query("SET NAMES utf8");
and while you're at it, change the MySQL table collation to UTF8_unicode or UTF8_general
You can even add some magic to .htaccess:
AddDefaultCharset utf-8
I've had these charset problems plenty of times, so I know where to find them :)
I'm hoping for an explanation of why some UTF-8 text is being saved to a database table incorrectly...
I created an HTML form and the page's meta content is set to UTF-8:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
The PHP and template files are all Unicode/UTF-8.
The form field data is submitted to a utf8_unicode_ci encoded database table.
If I submit the form with characters such as "éçä" (which I created from Windows' Character Map program set to Unicode character set) they show up incorrectly in the database ("éçä"). I'm viewing the database via phpMyAdmin (which is also set to UTF-8 character encoding).
However, if I run iconv() on the string to convert to ISO-8859-1 before inserting it into the database, then the character show correctly:
$input = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $input);
What is going on? Shouldn't the fact that everything is UTF-8/Unicode from beginning to end resulted in it being correct in the DB? What am I doing wrong and why did converting the data to ISO-8859-1 work?
The only other thing done to the data is a FILTER_SANITIZE_MAGIC_QUOTES:
$input = filter_var($input,FILTER_SANITIZE_MAGIC_QUOTES);
Thank you for your time and input.
Two steps you haven't mentioned:
Specify UTF-8 in HTTP Content-Type header
Specify UTF-8 when connecting to MySQL, e.g. specifying charset in PDO
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 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!
I keep getting these weird text characters when I display user submitted text. like in the following example below. Is there a way I can fox this using PHP, CSS or something so that the characters are displayed properly?
Here is the problem text.
Problems of �real fonts� on the web. The one line summary:
different browsers and different platforms do �hinting�
Here is my meta tag.
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
It's an encoding problem. Make sure you send the correct encoding to the browser. If it's UTF-8, you'll do it like this:
header("Content-type: text/html; charset=utf-8");
Also, make sure that you store the content using the same encoding throughout the entire system. Set your database tables to utf8. If you're using MySQL, run the SET NAMES utf8 query when connecting to make sure you're running in UTF-8.
These weird characters occur when you suddenly switch encoding.
Also, some functions in PHP take a $charset parameter (e.g. htmlentities()). Make sure you pass the correct charset to that one as well.
To make sure that PHP handles your charset correctly in all cases, you can set the default_charset to utf-8 (either in php.ini or using ini_set()).
Set your page to UTF-8 encoding.
Please check with the char-set in header section.
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
use this below one:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
or try this one:
htmlentities($str, ENT_QUOTES);
Could be problem with file encoding please check that your files is correctly encoded, saved as "UTF-8 without boom", also if you are saving to database use SET NAMES UTF-8