dsafgdafgdf45345 2013-08-04 13:16
浏览 593
已采纳

regexp_replace中的转义字符串 - Postgresql和PHP

I'm importing a csv-file with user-data another person sent me. The file was sent back-and-forth between multiple people, working with different programs on different OS. So some encoding issues occurred and I found no easy way to fix it (yes I converted the file to UTF-8 etc).

The data are stored in a postgresql database and I found a way to convert the broken characters.

For example:
â\u0088\u009Aº = ü
â\u0088\u009A§ = ä
â\u0088\u009Aâ\u0088\u0082 = ö
...

In psql I'm using the following statement to convert â\u0088\u009Aº to ü:

SELECT COUNT(*) FROM sbl_students WHERE lastname ~* E'.*â\\u0088\\u009Aº.*';

=> 61

UPDATE sbl_students SET lastname = regexp_replace(lastname, E'â\\u0088\\u009Aº', 'ü', 'g');
SELECT COUNT(*) FROM sbl_students WHERE lastname ~* E'.*â\\u0088\\u009Aº.*';

=> 0

I tried to implement this into a php script, but for some reason it doesn't work. Nothing is replaced and no error occurs.

$dbh = pg_connect("dbname=iserv user=sbl");
$query = "UPDATE sbl_students SET lastname = regexp_replace(lastname, E'â\\u0088\\u009Aº', 'ü', 'g');";
pg_query($dbh, $query);
pg_close($dbh);

I already tried multiple variations like regexp_replace(lastname, E'â\u0088\u009Aº', 'ü', 'g') or regexp_replace(lastname, 'â\u0088\u009Aº', 'ü', 'g') but non of them worked.

Can anybody help me out? I don't understand why the query works great in psql but doesn't do anything in php.

Thanks!


Postgresql 8.4
PHP 5.3.3-7+squeeze16 with Suhosin-Patch (cli)

  • 写回答

2条回答 默认 最新

  • dongrouli2667 2013-08-04 14:51
    关注

    Found the answer myself. You have to use pg_prepare():

    $dbh = pg_connect("dbname=iserv user=sbl");
    pg_prepare($dbh, "", "UPDATE sbl_students SET lastname = regexp_replace(lastname, $1, 'ü', 'g');");
    pg_execute($dbh, "", array("â\u0088\u009Aº"));
    pg_close($dbh);
    

    Still don't understand why :/

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器