pankil thakkar
pankil thakkar

Reputation: 411

convert unicode to string php

My actual string is

Mom said, "I love this one." Jake said, "I don’t get it."

But instead it

In my database in one column the value is saving like that i cant change

Mom said, “I love this one.†Jake said, “I don’t get it.â€

I am fetching that from query and do json_encode then it display the output like this in json string

Mom said, \u00e2\u20ac\u0153I love this one.\u00e2\u20ac\u009d\n\nJake said, \u00e2\u20ac\u0153I don\u00e2\u20ac\u2122t get it.\u00e2\u20ac\u009d\n

So please tell me how to decode this unicode into double quotes.

thanks in advance

Upvotes: 1

Views: 5485

Answers (2)

Tino Didriksen
Tino Didriksen

Reputation: 2255

The data that you already have probably needs to be passed through utf8_decode() (not utf8_encode()).

To ensure smooth sailing in the future, you should make sure your whole pipe is UTF-8. This is non-trivial, sadly. Off the top of my head, places that need to be checked:

  • HTTP daemon's default encoding
    • For Apache, add to .htaccess or httpd.conf: AddDefaultCharset UTF-8
  • PHP's default_encoding
    • Add in .htaccess or server config: php_value default_charset "UTF-8"
  • HTTP's header content-type charset
    • header('Content-Type: text/html; charset=UTF-8');
  • HTML's meta content-type charset
    • <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  • PHP's MySQL connection charset
    • mysql_set_charset('utf8');
  • MySQL's database default charset and collation
    • Use charset utf8 and collation utf8_general_ci
  • MySQL's table and column charset
    • Use charset utf8 and collation utf8_general_ci

And if you need to output values that may contain unwanted HTML or XML, use htmlentities($var, ENT_COMPAT, 'UTF-8'); instead of simply htmlentities($var);

Also, use the mb_* functions where possible, such as mb_strlen() and mb_substr().

Upvotes: 3

Sahal
Sahal

Reputation: 4146

Change your table collation to latin1_swedish_ci

With special char you can also do something like this

for displaying the data from mysql

$str=html_entity_decode(stripslashes($rows[4]));

for inserting the data with special characters into mysql

$var_name=htmlentities(mysql_real_escape_string($detail));

Upvotes: -1

Related Questions