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 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿