dousui8263 2015-12-09 20:48
浏览 157
已采纳

转换数据库数据以获得htmlspecialchars兼容性

So, my server was recently upgraded to PHP 5.5, and htmlspecialchars() is returning blank for some inputs. This is because PHP changed the function to expect UTF-8 encoded strings by default.

Instead of changing each and every instance of htmlspecialchars in my multiple, large websites... I was hoping instead to convert the strings BEFORE they reach this function. Namely, in the mysql database. My application is an internal MVC framework, so it loads data from the DB into an Articles class. Then the class object is used in various pages on the site like so: echo( htmlspecialchars( $article->title ) ); etc.

My mysql database is currently using MyISAM tables with an latin1_general_ci collation.

When I use PHPMyAdmin to change to collation on the Articles table to utf8_unicode_ci .... it still does not solve the problem. I can't understand why the data is not now in the proper encoding for htmlspecialchars?

Any suggestions?

(Note, I am aware of runtime solutions to rewrite the htmlspecialchars function as a work-around but this is not possible on my shared host.

I am also aware that I could search and replace across the entire site to pass in the desired encoding to every occurrence of the function, but since my site is large and several of them are encoded / go through other build process, changing this many files is a non-starter. )

Thank you!

  • 写回答

2条回答 默认 最新

  • douyalin0847 2015-12-09 21:25
    关注

    So... some tinkering revealed that setting my DB connection object's charset to utf8, fixes it, even if the DB tables are still latin1_general_ci. This works for reading from and writing to the DB, so the conversion must be happening properly.

    $dbc->set_charset("utf8");  // Using mysqli built in PHP library.
    

    Note - I'm only working with english and french chars.

    I will use this (along with setting the HTML page header charset to utf-8) as a temporary solution until I can convert the master copy of my application to use utf-8 everywhere.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?