How to convert String with “ (ISO-8859-1) characters to normal (UTF-8)characters?

29,886

Solution 1

$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.

Solution 2

“ 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.

Solution 3

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

Share:
29,886
muthukrishnan
Author by

muthukrishnan

Updated on August 29, 2021

Comments

  • muthukrishnan
    muthukrishnan over 2 years
    <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