Accettend letter and other graphic simbols PHP->JS - php

I have to read a txt via file php. This file contains some normal so may contains this kind of symbols :
€ é ò à ° % etc
I read the content in php with file_get_contents and transform these for inserenting in SQL database.
$contFile = file_get_contents($pathFile);
$testoCommento = htmlspecialchars($contFile,ENT_QUOTES);
$testoCommento = addslashes($testoCommento);
Now if I have this text for example :
"l'attesa �é cruciale fino a quando il topo non viene morso dall'�€"
in the database I have this:
l'attesa è cruciale fino a quando il topo non veniene morso dall'€
When I was GETTING the data from the database I use the php function for decode html entites
$descrizione = htmlspecialchars_decode($risultato['descrizione'],ENT_QUOTES);
$descrizione = addslashes($descrizione);
Now I use jasvascript and AJAX for getting the table content and display to an HTML page
In the browser instead of getting the correct text (€,è) I have square symbol.
I think there is some mess with charset code/decode but never figured out.
The SQL' table is in "utf8_unicode_ci" format and the column in "utf8_general_ci".
The content-type of the page is
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Thanks for help me!

addslashes() is not Unicode-compatible, you should use a different way to escape the quotes in the strings, or (which would be much better) switch to using prepared statements instead of constructing the SQL query as a string.
You can find more details at: http://eleves.ec-lille.fr/~couprieg/post/Bypass-addslashes-with-UTF-8-characters

Related

Encoding error causes MS SpecialChars to be shown as question marks

So when I retrieve a column from my database and echo the string in php it displays microsoft special chars as this �.
However if I copy the string from the cell, paste it into my script and echo it directly, I get the correct content.
At what stage between retrieving the data and displaying it is this going wrong?
It is an MS SQL database. If any more information on current set up will help, let me know and I will supply it, it's just at this point I'm not sure what will be usefull and what wont.
It's down to encoding.
Is your database table encoded in the same character set as the PHP document, e.g. UTF=8?
If you can, either change the database table's collation or the PHP document's encoding, either with
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
or
header('Content-Type: text/html; charset=utf-8');
with the correct character set.

Diaeresis show up as question marks when read from MSSQL database with PHP

I have a bit of PHP that gets data from a MSSQL database and saves it to an array:
while($row = mssql_fetch_array($dbquery, MSSQL_NUM))
{
$content = array(
'something' => $row[1],
'something_else' => $row[2]
// etcetera
);
}
(The reason why I write the data to an array like that is not relevant I think, but if you must know: it is because the data is parsed by ExpressionEngine (EECMS). The PHP code is part of a plugin I am developing and this is the easiest way to make the data available for use in ExpressionEngine tags.)
Some of the data in de MSSQL database contains punctuated characters, such as ë and é. The diaeresis (ë) are shown as question marks in the HTML. For instance, Cliënten is shown as Cli�nten.
The HTML is in UTF8: <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
I cannot change anything in the MSSQL database. What is the best way, preferably in PHP, to fix this?
When you see question marks like that, it means your browser is having trouble understanding what the character the question mark has replaced is supposed to represent. Most likely, it is expecting utf8 character encodings and is not getting them.
Try running something like this on your array (after it's built) and see if that helps:
$content = array_map('utf8_encode', $content);
Basically you want to run the utf8_encode() function on all the strings that come out of the database since apparently they are not encoded as such in the database.
You could also run the utf8_encode function immediately before you output each variable.
So something like:
<p><?php echo utf8_encode($content_row) ?></p>

Varbinary encoding

I got a varbinary field in my database, and got some problems with displaying special (Polish) characters like ąśćężźć.
Example: SELECT local_name from items WHERE id = 140 returns: Pieczęć, the problem appears when I want to print this data on my website (encoding UTF-8 there), then the Pieczęć turns into the following string: Piecz�� tried also to use utf8_encode() PHP function but it gives the following result: Pieczêæ.
How can I solve that so it will print the special characters without problem?
same adivce as here:
https://stackoverflow.com/a/11254131/1489924
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
and/or
SET NAMES 'utf8'
worked for me in most situations.
Good luck!

Character set encoding issue

All, Im having the age old problem with character encoding...
I have a mySQL DB with a field set to utf8_unicode_ci. My PHP page as the header entry <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />. When I use a simple form to POST data with Cyrillic characters to the DB, e.g. 'гыдлпоо', the characters display correctly in the textarea, and are added to the DB where they display correctly.
When fetching the characters from the DB, my page only displays a series of question marks. I've used mb_detect_encoding($content, "UTF-8,ISO-8859-1", true); and the content is UTF-8, however the characters do not display.
I've searched around (including on SO) and tried any number of solutions, to no avail- any help would be much appreciated.
Many thanks
Do this right after mysql_connect() and mysql_select_db():
mysql_query("SET NAMES 'utf8'");
Try using mysql_set_charset() function before fetching data from database.
did you try to use the form with
enctype="multipart/form-data"
?
this might help.. it's not necessary for the text to be readable in your database.. when they are saved they should be utf8 encoded.. you need them to look fine when you output the string again

Change the characters in mysql with Convert failing - Still getting Não

I am populating this mysql table with data from a php (via post and using filter_input).
The database is utf8 but when I have a user that inputs words with ^,',',~ like Não I get this -> Não
What do I have to do to make it show the correct values. Or should I try to make some correction when I retrieve the data??
UPDATE:
I have added a utf8_decode and now it is inserting ok.
Anyone know how to convert the string that were already in the table?? I tried using the convert function but I can't make it work :(
UPDATE:
I am trying this code:
select convert(field using latin1)
from table where id = 35;
And I am still getting this: Não
I tried other encoding s but I never get the word Não
Anyone have any thoughts on this one??
First, make sure your page is utf-8
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
next, if your on Apache, make sur your in UTF-8 in config file :
AddDefaultCharset UTF-8
or your can do it in a .php file like this :
header('Content-type: text/html; charset=UTF-8');
if you still have problem, you can use the encode function :
$value = utf8_encode($value);
Hope all this will help...
It looks like somewhere along the way something cannot handle Unicode. As a result, ã is getting interpreted as two separate characters. Make sure everything that handles strings is OK with Unicode.

Categories