Skip to main content

Forums

This topic contains 4 replies, has 3 voices, and was last updated by  __ 4 years, 2 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #202666

    bearhead
    Participant

    I’m trying to use this php to replace characters in a text string I’ve taken from a mysql database:

    str_replace(array('“', '”', '’', '®', '—'), array('"', '"', "'", '®', '-'), $row{'description'});

    Instead of seeing the characters I’m trying to use as replacements, I’m just seeing missing character symbols when the page renders in browser.

    If I run that same str_replace code on a string not taken from mysql, it works as expected.

    The “description” field in mysql is set to use character set: utf8

    what am I missing?

    #202692

    ovi
    Participant

    Make sure the PHP file where you use this function is encoded as UTF-8.

    #202715

    __
    Participant

    This reply has been reported for inappropriate content.

    1) what do you see in the actual text of the output string (e.g., if you “view source”)?

    2) don’t do this. There’s a built-in function for it.*

    $description = htmlspecialchars( $row['description'],ENT_QUOTES,"UTF-8" );
    

    * well, not for ® or . but why are you encoding these?

    #202854

    bearhead
    Participant

    This reply has been reported for inappropriate content.

    The page is definitely encoded as UTF-8.

    I was trying to encode the register mark and em dash, because they are not rendering in browser, so I thought that maybe those characters aren’t recognized unless they are encoded? But now I know that is not the case… those characters render fine if they are not pulled from my database.

    edit:

    I needed to set the character set in php:

    $mysqli->set_charset("utf8");

    So I guess the page wasn’t encoded as UTF-8? Which is weird, because I checked the encoding in FF and Chrome, and both were reporting that is was UTF-8.

    #202959

    __
    Participant

    This reply has been reported for inappropriate content.

    no, that’s the charset used by the connection to your database. DB, conn, content-type, actual charset used… they all have to match. That way, you’ve got plenty of opportunities to screw it up!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

icon-link icon-logo-star icon-search icon-star