dousi6405
2012-05-04 12:44
浏览 8
已采纳

是否有PHP函数将数据库中突出显示的字母转换为html代码?

There is a MySQL database containing data with accentuated letters like é. I want to display it in my PHP page , but the problem is that there are unrecognized characters displayed because of the accent. So is there a function to convert accent to HTML code , for example é is converted to é !

图片转代码服务由CSDN问答提供 功能建议

有一个 MySQL 数据库,其中包含强调字母的数据 比如é。 我想在我的 PHP 页面中显示它,但问题是由于 accent 而显示无法识别的字符。 那么有一个 function 来将重音转换为HTML代码,例如é转换为& eacute;

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duanqianruan8448 2012-05-04 12:49
    已采纳

    Rather than using htmlentities you should use the unicode charset in your files, e.g.

    <?php
    header('Content-Type: text/html; charset="utf-8"');
    

    To be on the safe side, you can add the following meta tag to your html files:

    <html>
      <head>
        <meta charset="utf-8" />
    

    Then, make sure that your data base connection uses utf8:

    mysql_connect(...);
    mysql_select_database(...);
    
    mysql_set_charset('utf-8');
    

    Then, all browsers should display the special characters correctly.

    The advantage is that you can easily use unicode characters everywhere in your php files - for example the copyright sign (©) or a dash (–) - given that your php files are encoded in utf-8, too.

    点赞 评论
  • duanhuanyou6478 2012-05-04 12:47
    点赞 评论
  • dongna2498 2012-05-04 12:51

    you can easily make one yourself with str_replace:

    function txtFormat($input){
    
        $output = str_replace('/\à/','&#201;',$input);
        $output = str_replace('/\è/','&#232;',$output);
        $output = str_replace('/\é/','&#233;',$output);
        $output = str_replace('/\ì/','&#236;',$output);
        $output = str_replace('/\ò/','&#242;',$output);
        $output = str_replace('/\ù/','&#249;',$output);
    
        return $output;
    
    }
    
    点赞 评论
  • dongqie5529 2012-05-04 12:52

    Use the following code

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    

    And don't encode your data via utf8_encode() function before inserting into database hope this will solve your problem :)

    点赞 评论

相关推荐 更多相似问题