douchen1988 2013-05-08 09:00
浏览 45
已采纳

PHP使用重音保存字符串

I'm having an issue when trying to save strings with accents.

Our online system is used by clients from different countries, so some of our clients use accents.

When doing a request to get the value to save like this

$value = trim(strip_tags(htmlspecialchars($_REQUEST['value'], ENT_QUOTES, 'UTF-8')));

It saves the value in this way

Gé

If instead of htmlspecialchars I use htmlentities it saves like this

géî

The correct string I'm trying to save is

I need this to be saved correctly so it can be displayed correctly on screens and reports.

Our db uses UTF-16 (MS SQL Server), and I'm declaring the meta tags to use UTF-8 on the page like this

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

This is how the connection looks:

$link = sqlsrv_connect($dbserver, array(
    'Database'=>$database,
    'UID' => $uid,
    'PWD' => $pwd,
    'CharacterSet' => 'UTF-8'
));
  • 写回答

2条回答 默认 最新

  • doujinge9648 2013-05-08 15:31
    关注

    After some trial and error, this is the solution I've found.

    sqlsrv_connect does by default a connection pooling, so the connection remains in the application pool.

    By default, the sqlsrv_connect() uses connection pooling to improve connection performance. To turn off connection pooling (i.e. force a new connection on each call), set the "ConnectionPooling" option in the $connectionOptions array to 0 (or FALSE). For more information, see » SQLSRV Connection Pooling.

    From PHP Manual - sqlsrv_connect

    My connection used to be like this

    $link = sqlsrv_connect($dbserver, array(
        'Database'=>$database,
        'UID' => $uid,
        'PWD' => $pwd
    ));
    

    Without the characterset.

    Even after I added the characterset to the connection, I still experienced this errors.

    This is my connection now.

    $link = sqlsrv_connect($dbserver, array(
        'Database'=>$database,
        'UID' => $uid,
        'PWD' => $pwd,
        'CharacterSet' => 'UTF-8'
    ));
    

    After I forced the browser to use UTF-8 by using the meta tags it was saving correctly to the db but displaying incorrectly. This was because I was encoding to UTF-8 twice, I then removed the second encoding when displaying.

    After all this I did an IIS Reset to reset the application pool, and forced on the scripts to close the connection after processing like this:

    // Close the connection.
    sqlsrv_close( $linkEventlogic );
    

    It now works correctly.

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

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路