Rahul Singh
Rahul Singh

Reputation: 1632

Storing utf8 data`मेरा भारत महान` in database

I want to store UTF8 in database. I have data in Unicode Hindi and I want to store in MySQL database using php after converting it to HTML character sets. let's say someone enters a bullet (•) character into a text box. When saving that data, should it be converted to •.

Suppose I have data मेरा भारत महान I want to store it in database by converting it to html character. How can I do that? I tried to use htmlentities function but that doesn't work satisfactorily for me.

Upvotes: 4

Views: 597

Answers (2)

hakre
hakre

Reputation: 197692

The • thingies are called HTML Entities. In PHP there is a function that can create these: mb_encode_numericentityDocs, it's part of the Multibyte String extension (Demo):

$string = 'मेरा भारत महान';
$encoding = 'UTF-8';

$convmap = array(0, 0xffff, 0, 0xffff);
$encoded = mb_encode_numericentity($string, $convmap, $encoding);

echo $encoded; मेरा भारत महान

However: You need to know the encoding of your string. In this case I've chosen UTF-8, depending on it you need to modify the $encoding parameter of the function and the $convmap array.

However, don't store it that way into your database. Store it as-is and convert the output encoding after you retrieved the data from your database.

Similar Question: Convert (doublebyte) string to Hex

Upvotes: 3

Daniel Fekete
Daniel Fekete

Reputation: 5028

htmlentities has a charset parameter, Try: htmlentities($text, ENT_COMPAT, "UTF-8")

Upvotes: 0

Related Questions