0

I have a problem with string from my database mysql. I have a form with some input text and text area. I have a list of product with the button "Update". If I click this button the new page is so done:

<form action="modify.php">
<input type="text" name="name" value="<?php echo $name_from_db; ?>">
<br>
<input type="text" name="desc" value="<?php echo $desc_from_db; ?>">
<br/>
...
</form>

The problem are the lecters like "è", "à", ecc. In database arrive directely with that value and not with "&egrave;". But now in input form their value isn't correct.

I try to do this:

$desc_from_db= str_replace("è", "&egrave;", $desc_from_db);

Probably the charset (latin1_swedish_ci) it's different. What do I have to do?

EDIT I tried to do a solution and it seems work correctely. It isn't the best solution. Can you judge it? Could have some bug? The lecters dangerous are only 6: "à", "è", "ì", "ò", "ù", "é". So with the function: ord ($lecter) I notest that "è" have the code 232. So I could do (checking all the lecter of the word):

if(ord($lecter) == 232)
$word = substring($word, 0, $index)."&egrave;".substring($word, $index+1, strlen($word);
  • 1
    I think you need to read up on [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Naruto Nov 03 '15 at 09:20
  • yeah you need to set your database to UTF8-general-ci and also set your headers to UTF-8 – Puya Sarmidani Nov 03 '15 at 09:35
  • Yes, Otherwise whenever update or insert query before using below line: mysqli_set_charset($con,"utf8"); after you write your code. – MaThar Beevi Nov 03 '15 at 09:38
  • @PuyaSarmidani I have set it but the situation does not change – all_key_the Nov 03 '15 at 09:38
  • you are making a mysql connection. i recommend using PDO or mysqli and then add this to the header. mb_internal_encoding('UTF-8'); mb_http_output('UTF-8'); mb_http_input('UTF-8'); mb_language('uni'); mb_regex_encoding('UTF-8'); – Puya Sarmidani Nov 03 '15 at 09:51
  • Don't use Latin1. We're in the 21st century now. (And have been for a decade and a half!) – Quentin Nov 03 '15 at 10:04
  • I use it only because standard. If I use utf8-general-ci do I have to set utf8 as meta tag? – all_key_the Nov 03 '15 at 10:07

0 Answers0