PhpSrbijaBgd PhpSrbijaBgd - 19 days ago 5
PHP Question

Escaping characters before insert into database

I want to convert some characters before inserting them into database for example

Ž, Ć

I get data from post, and sanitize it on this whay

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


In some reason I get still Ž, Š into my database

Answer

From http://php.net/manual/en/function.htmlentities.php

all characters which have HTML character entity equivalents are translated into these entities

If you look at http://www.ascii.cl/htmlcodes.htm

Ž, Ć don't have HTML character entity equivalents. Š does. Which is why

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

Produces:

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