如何将文字\u序列转换为UTF-8? [英] How to convert literal \u sequences into UTF-8?

查看:850
本文介绍了如何将文字\u序列转换为UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从外部源加载数据转储,并且某些字符串包含UTF8字符的 \uXXXX 序列,例如:

I am loading data dump from external source and some strings contain \uXXXX sequences for the UTF8 chars, like this one:

\u017D\u010F\u00E1r nad S\u00E1zavou

我可以通过使用 psql 中的E''常量来检查内容,但是找不到任何函数/运算符返回我适当的值。

I can check the contents by using E'' constant in psql, but cannot find any function/operator to return me proper value.

我想问一下,是否有可能使用PL / pgSQL函数将带有Unicode转义符的字符串转换为普通的UTF8

I'd like to ask, if it's possible to convert this string with unicode escapes into normal UTF8 without using PL/pgSQL functions?

推荐答案

我认为没有内置的方法。我想到的最简单的方法就是您要避免的plpgsql函数:

I don't think there is a built in method for that. Easiest way I can think of is the plpgsql function you wanted to avoid:

CREATE OR REPLACE FUNCTION str_eval(text, OUT t text) AS
$func$
BEGIN
EXECUTE 'SELECT E''' || replace($1, '''', '''''') || ''''
USING $1
INTO t;
END
$func$ LANGUAGE plpgsql IMMUTABLE STRICT;

更新后的版本可防止 SQLi ,而且速度也更快。

The updated version safeguards against SQLi and is faster, too.

这篇关于如何将文字\u序列转换为UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆