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 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口