nobody
nobody

Reputation: 31

is it utf-8 problems? php mysql

I am getting stuck with this, previously i am using php 5 and now i came up with php 7,

the problem is when i am trying to echo value from database it returns weird special character and where in my previous web it returns normal. Is it utf-8 problem? i tried meta tag utf-8, and change collation sql into utf8_unicode_ci, but somehow it doesn't help at all...

it returns like this

 — 

what i want to return

 — 

Upvotes: 2

Views: 64

Answers (1)

Paolo
Paolo

Reputation: 15827

What you get from the database is a UTF8 encoded string.

The characters you see is a UTF8 string interpreted with encoding Western (Windows Latin 1).

If you include that string in a web page whose character set is Latin 1 then you'll see the string you posted; if the character set is UTF-8 then you should see the correct characters (without need to convert them into HTML entities).

As the latter is not your case you can proceed as follows:


Let the characters you see are stored in the variable $string: you can get html entities with mb_convert_encoding:

$html = mb_convert_encoding( $string, 'HTML-ENTITIES', 'UTF-8' );

This will result in:

 — 

As after conversion you get characters in the ASCII range then the resulting string is suitable for any destination character encoding.

Note that, according to the above, even the dash is converted (into —)


This is just a quick solution to the problem you faced.

I think the comment from Machavity:

"Take a minute and read stackoverflow.com/questions/279170/utf-8-all-the-way-through"

is a good advice.

Upvotes: 2

Related Questions