2

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

 — 
nobody
  • 31
  • 3
  • 3
    Take a minute and read https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Machavity Mar 23 '18 at 20:18
  • The character set needs to be UTF8. Entities are all ascii characters so latin1 can hold them. ` — ` looks like the single characters of a multibyte character. – chris85 Mar 23 '18 at 20:19

1 Answers1

2

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.

Paolo
  • 15,233
  • 27
  • 70
  • 91
  • hey it works with **mb_convert_encoding( $string, "Windows-1252", "UTF-8" )** thank you Paolo, is it possible to encode it all without using mb_convert_encoding ? – nobody Mar 23 '18 at 21:46
  • @Fay yes. The secret is to read https://stackoverflow.com/questions/279170/utf-8-all-the-way-through and avoid breaking your data in the first place. – Sammitch Mar 23 '18 at 22:43
  • @Fay are you sure?? ` — ` **cannot** be represented by **Windows-1252** encoding ---- `mb_convert_encoding` is the easyest way to to the job (afaik) – Paolo Mar 23 '18 at 22:43