What should I do with funky '’' characters?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aaron718
    New Member
    • Aug 2010
    • 2

    What should I do with funky '’' characters?

    I use the code below to replace these funky characters when they end up in the database. In phpmyadmin the queries work just fine, however, when I use something similar in php, they don't throw any errors and they also don't work.

    In mysql:
    Code:
    UPDATE news SET title=(replace (title, '’','\''));
    UPDATE news SET description=(replace (description, '’','\''));
    UPDATE news SET title=(replace (title, '‘','\''));
    UPDATE news SET description=(replace (description, '‘','\''));
    UPDATE news SET title=(replace (title, ''','\''));
    UPDATE news SET description=(replace (description, ''','\''));
    UPDATE news SET title=(replace (title, '&','\&'));
    UPDATE news SET description=(replace (description, '&','\&'));
    UPDATE news SET title=(replace (title, '–','-'));
    UPDATE news SET description=(replace (description, '–','-'));
    In php:
    Code:
    $find[] = '“'; // left side double smart quote
    $find[] = '”'; // right side double smart quote
    $find[] = '‘'; // left side single smart quote
    $find[] = '’'; // right side single smart quote
    $find[] = '…'; // elipsis
    $find[] = '—'; // em dash
    $find[] = '–'; // en dash
    
    $replace[] = '"';
    $replace[] = '"';
    $replace[] = "'";
    $replace[] = "'";
    $replace[] = "...";
    $replace[] = "-";
    $replace[] = "-";
    
    $cleanQuery = "SELECT * FROM news ORDER BY id DESC LIMIT 50";
    $runSelect = mysql_query($cleanQuery) or exit(mysql_error().$cleanQuery);
    while($tickerResults = mysql_fetch_assoc($runSelect)) {
        $saveWeather = "UPDATE news SET title = '".mysql_real_escape_string(str_replace($find, $replace, $tickerResults['title']))."', description = '".mysql_real_escape_string(str_replace($find, $replace, $tickerResults['description']))."' WHERE id = '".$tickerResults['id']."'";
        //exit($saveWeather);
        $runInsert = mysql_query($saveWeather) or exit(mysql_error().$saveWeather);
    }
    What am I doing wrong here?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    as this is a character set problem, you should rather find the reason why it changes the encoding. for instance you might not have configured the DB connection to use UTF-8.

    Comment

    • Aaron718
      New Member
      • Aug 2010
      • 2

      #3
      I'm parsing in rss feeds, at which point the characters are turned into that mess by php... This has really boggled me for a while now.. I've tried a few utf-8 conversion attempts with no success.

      Comment

      • rehandev
        New Member
        • Aug 2010
        • 3

        #4
        U have to convert your DB into UTF-8. And try to save all the entries again. This would save different language data properly.

        Also, on the site, you can put language code in META TAB. So, it would show that language data properly on web page.

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          PHP dont make mess with encoding at all. I have to deal with Japanese language with two different encoding. UTF-8 for normal website and shift-jis for mobile web-site.

          You better make sure you are not mixing encoding. And use a good text editor.

          And yes Database is configured for UTF-8. But I think even if the database base is configured as UTF-8. But you can save different Encoding in the table. Cause whatever encoding you use its all binary data and size is divisible by 8 with no carrige.

          And to replace escape character use
          mysql_real_esca pe_string it will save your energy

          Comment

          Working...