Yet another UTF-8, PHP and Ajax post - php

I've tried to solve an issue with character encoding for many days now without finding any solution.
Here's what's happening:
I have a form in a page.
When I copy paste a text from Adobe Reader to this form, everything goes fine.
When I copy paste a text from Preview (mac os image viewer), it turns into strange characters.
When the form is submitted, the sentence:
salade mêlée, tomates, mozzarella, basilic melon en saison et jambon cru
Goes through an ajax function and I can see in firebug:
salade%20me%CC%82le%CC%81e%2C%20tomates%2C%20mozzarella%2C%20basilic%20melon%20en%20saison%20et%20jambon%20cru
Now when I get this value into my Zend Controller, in order to save it to my database, I meet the following cases:
if i iconv it to cp1252, the text is cut to "salade me" and that's it
If if utf8_encode it transforms into: salade meÌleÌe, tomates, mozzarella, basilic melon en saison et jambon cru
If I utf8_decode it, it goes to: salade me?le?e, tomates, mozzarella, basilic melon en saison et jambon cru
If I do no transformation, it works...but in phpmyadmin i see: salade mêlée, tomates, mozzarella, basilic melon en saison et jambon cru
Any idea to help me? I'm turning crazy!!
Thanks!

Make sure that phpMyAdmin is configured to use UTF-8, and that the database is also using UTF-8, as well as the connection between PHP and the database. If all of them are using UTF-8, then you should have no issues passing UTF-8 back and forth.

Related

HTML Special Characters Post as ASCII

Please forgive me, I am new here. I hope I have formatted this correctly. I have converted a database from ASCII to UTF-8 changing all of the Special Characters. Example â changed to â
Working Example:
Domaine Comte Georges de Vogüé
changed to,
Domaine Comte Georges de Vogüé
In my HTML page I have a form with the line below as one of the options.
<option>Domaine Comte Georges de Vogüé</option>
When the form is posted to the PHP page the value is changed to
Domaine Comte Georges de Vogüé
So when it is searched for in the database of course it is not found.
The options for the dropdown field are generated dynamically using code I found at [So You Need To Fill a Dropdown Dynamically https://css-tricks.com/dynamic-dropdowns/]
How do I keep the option value from changing when posted to the PHP script?
Did you try setting a value to the option?
<option value="Domaine Comte Georges de Vogüé">Domaine Comte Georges de Vogüé</option>
You need to convert the HTML entities as displayed on the page (ü, é, etc.) to their representative values prior to performing the database search; this can be accomplished using the PHP function html_entity_decode() -- for example:
<?php
print html_entity_decode('Domaine Comte Georges de Vogüé');
?>
Will result in an output of --
Domaine Comte Georges de Vogüé
More information, options, and examples of usage can be found within the PHP manual -- http://php.net/manual/en/function.html-entity-decode.php

Hidden "space" breaks PHP Object property name after retrieving SQL result

I was having an issue where I could get an object back from a Codeigniter query, run print_r($object) and it would show:
stdClass Object ( [client_id] => 4105 [name] => William Hilliard ...
However, when I tried to print $object->name, nothing printed to the screen. Using Codeigniter's result_array() function to retrieve the result fixed the ability to print the value, but still did not explain why an Object property would not print to the screen. Even then, I wanted an object, not an array. Eventually, I went to the SQL I was running:
SELECT
t.client_id as client_id
,CONCAT_WS(' ',t.first,t.last) as name
,t.foo as bar...
When I quoted name as `name` with tildes, the problem was fixed. This made me curious, so I downloaded BBEdit and looked at the line. In every other text editor, there was no space at the end of the line, but deleting and rewriting the line would fix the problem. BBEdit showed something different:
Δ Δ Δ Δ ,CONCAT_WS(' ',t.first,t.last)·as·name ¬
It clearly shows a space between name and ¬. Copying and pasting this
"whitespace" resulted in nothing pasting in text editors and web browsers. However, deleting the character and saving the file in BBEdit would fix the problem in PHP. Can anyone tell me what character this "phantom space" is and a possible way that a developer could have ended up with this garbage in their code?

Unable to save text to database with 'body' => htmlentities($_POST['body']) on server, but on localhost it works

I have an array called $data and I am sending this data to my db insert method.
This works nice when used with:
'body' => $_POST['body']
However, when I try:
'body' => htmlentities($_POST['body'])
Only a first 3 words are saved but when it comes to character ě it stops there and other parts of the text is not saved.
in my db body is stored in body column which is a standard text type, utf_8_general_ci.
It has nothing to do with db settings I guess, because all tables is set to uf8_general_ci as well as the table itself (the same as on my localhost).
The funniest part is that on my localhost machine running wamp it wokrs with al these weird characters and the text is saved correctly when sing htmlentities.
Only on the server it didn't work. Btw. it's justhost if it helps.
Do you know how to run it the same way as on my localhost or any other function that can be used except htmlentities ?
I need htmlentities because I have html code like google maps and it needs to be saved. Without htmlentities it is saved incorrectly.
Try using mysql_real_escape_string or mysqli_real_escape_string or prepared statements when inserting into a DB.
Try and set the htmlentities encoding to match your form data encoding.
Note the dividing PHP version 5.4.0: default encoding for htmlentities can be UTF-8 or ISO-8859-1. Does your local machine have the same PHP version as server?
Note to all local machine users (MAMP, WAMP, LAMP, whatever): always check your PHP/MySQL versions if they match the server version. There is almost always some common function that doesn't work as expected when the testing environment isn't identical to production environment (been there, done that – once or twice...).
You can use set utf8 to mysql before insertion. It can insert special character in the database. FYI http://php.net/manual/en/function.mysql-set-charset.php

PHP working with text encoding

I am working on the Facebook Public Search API.
As you may understand the results I get come for many different sides of the world.
What I have to do is to give to all the texts I get the same text encoding before putting it inside my MongoDB. I need to use UFT8 as a general and working encoding.
This is an example of what I may get from Facebook:
10 ผู้นำที่โลà¸à¹„ม่ปรารถนา หาà¸à¹„ม่มีผู้นำประเภทนี้à¹à¸¥à¹‰à¸§à¹‚ลà¸à¹€à¸£à¸²à¸à¹‡à¸ˆà¸°à¸”ีขึ้นเยอะ โดยไทยติดอันดับ 1 อ่านต่อได้ที่นี่
or
Now he says he’d side with Pakistan if there were a conflict with the U.S. Better than the Taliban for sure, but not by much. The poor people of Afghanistan… Ayman al-Zawahiri: Al-Qaeda’
or
€™esercito tedesco, il primo modello di A400M è in fase di collaudo, e ci resterà per tre anni
Is there a function in PHP that can quickly convert the text into a UFT8 text encoding?
Did you try this function?
http://php.net/manual/en/function.utf8-encode.php

Search and replace in MySQL database?

I have an unusual problem (this is linked to Browser displays � instead of ´)
I had mismatched character encoding settings on my server (UTF-8) and application (ISO-8859-1), so a third person tasked with entering Spanish translations, entered the words properly at his end, but they weren't saved correctly in the database.
I have subsequently fixed the problem and the server is now ISO-8859-1 as well. [I set
default_charset = "iso-8859-1"
in php.ini]
I do see a pattern in what is in the system, for example the following appears on the system:
Nombre de la organización*
This needs to be:
Nombre de la organización*
ie, I need to search and replace 'ó' with 'ó'.
How can I do so for an entire table (all fields)? (there will be other such corrections as well)
Use the replace function. Simple example:
SELECT REPLACE('www.mysql.com', 'w', 'Ww');
Result: 'WwWwWw.mysql.com'
Now, if you have a table called Foo and you want to replace those characters in a field called bar, you can do the following:
update Foo set bar = Replace(bar, 'ó', 'ó');
Do this for all the affected fields and the problem is solved.
Best regards,
Lajos Arpad.

Categories