PHP - Fast way to strip all characters not displayable in browser from utf8 string

前端 未结 3 1518
迷失自我
迷失自我 2020-12-21 19:30

I\'ve got a little messy database containing names of many institutions around the world.

I want to display them including national characters, but without invalid c

3条回答
  •  被撕碎了的回忆
    2020-12-21 19:52

    The database might not be the problem entirely - if the tables are utf8 encoded the strings in them should have been converted (I think). The issue I've ran into with this has been a matter of correctly ensuring the encoding is consistent. For instance the mysqli connector, by default, reverts to Latin-8859 IIRC so it's quite possible to have the output in utf8, the database in utf8 and still end up with ? characters because they're converted to Latin by the mysqli connector.

    To ensure utf8 across the board you need to do something like:

    In the database:

    ensure the collation is something like utf8_general_ci

    At the top of the PHP view file:

    
    

    In the HTML meta tag (optional):

    
    

    AND in the database connector (using MySQLi as an example):

    mysqli::set_charset('utf8'); #note that for MySQL it isn't hyphenated
    

    You might find that resolves the problem anyway.

提交回复
热议问题