在插入数据库之前转义字符

时间:2015-02-18 13:53:55

标签: php

我想在将某些字符插入数据库之前转换它们,例如

Ž,Ć

我从帖子中获取数据,并在这个whay上清理它

$ime =  (string) htmlentities(mysql_real_escape_string($data['txtIme']), ENT_COMPAT,'UTF-8', true);

出于某种原因,我仍然将Ž,Š放入我的数据库

2 个答案:

答案 0 :(得分:1)

根据您要将其转换为的内容,mysql_set_charset()可能是您需要使用的功能。使用UTF-8编码,您也可以存储大多数非标准字符。

但是你应该小心使用mysql扩展,因为它已被弃用。您应该查看mysqliPDO

答案 1 :(得分:1)

来自http://php.net/manual/en/function.htmlentities.php

  

具有HTML字符实体等价物的所有字符都被翻译成这些实体

如果你看http://www.ascii.cl/htmlcodes.htm

Ž,Ć没有HTML字符实体等价物。 Š呢。这就是为什么

<?php
     $string = 'In some reason I get still Ž, Š, Ć into my database';
     echo htmlentities($string, ENT_COMPAT,'UTF-8', true);
?>

产地:

$ php utf.php
In some reason I get still Ž, &Scaron;, Ć into my database