7
<li>Jain R.K. and Iyengar S.R.K., “Advanced Engineering Mathematicsâ€, Narosa Publications,</li>

i have lot a raw html string in database. all the text have these weird characters. how can i convert to normal text for saving back it back in database.

$final = '<li>Jain R.K. and Iyengar S.R.K., “Advanced Engineering Mathematicsâ€, Narosa Publications,</li>';
$final = utf8_encode($final);

$final = htmlspecialchars_decode($final);

$final = html_entity_decode($final, ENT_QUOTES, "UTF-8");

$final = utf8_decode($final);

echo $final;

i tried above code, it displays correctly in web browser but still saving the same weird characters in database.

the charset of database is utf-8

muthukrishnan
  • 225
  • 1
  • 2
  • 15

3 Answers3

8
$final = '<li>Jain R.K. and Iyengar S.R.K., “Advanced Engineering Mathematicsâ€, Narosa Publications,</li>';

$final = str_replace("Â", "", $final);
$final = str_replace("’", "'", $final);
$final = str_replace("“", '"', $final);
$final = str_replace('–', '-', $final);
$final = str_replace('â€', '"', $final);

for past datas, i replaced the weird characters with UTF-8 characters.

for future datas, i made the charset to utf8 in php, html and databases connections.

muthukrishnan
  • 225
  • 1
  • 2
  • 15
7

“ is "Mojibake" for . You could try to avoid the non-ascii quotes, but that would only delay getting back into trouble.

You need to use utf8mb4 in your tables and connections. See this for the likely causes of Mojibake.

Rick James
  • 122,779
  • 10
  • 116
  • 195
3

It safer to use ftfy tool to fix texts https://ftfy.readthedocs.io/en/latest/

mahnunchik
  • 776
  • 3
  • 22
  • SQL to fix 5 cases, plus comment on two un-fixable cases: http://mysql.rjweb.org/doc.php/charcoll#fixes_for_various_cases – Rick James Aug 26 '20 at 22:50