Good time! I have such a question - I have a txt file with random text - I upload it to the server and use the file_get_contents () function to pull the contents of this file into a variable, then for example I want to display the contents on the screen or add it to the database without a difference, if my website (DB) works with utf-8 encoding and everything is correct in utf-8 document, if we say txt was created as usual in WINDOWS-1251, then naturally the text is obtained with cracks, is there a way to change the character encoding in the php variable ?
2 answers
This can be done in two ways:
$text = mb_convert_encoding($text, 'utf-8', 'cp1251');
$text = iconv('CP1251', 'UTF-8', $text);
Documentation: iconv () , mb_convert_encoding () .
- and if the original encoding is not known, can it be defined by the function mb_detect_encoding ()? and substitute in iconv? - dantelol
- This is a common method, but I have seen complaints that this function does not always work correctly. I myself, however, did not encounter problems. - Alexey Ukolov
- I myself am experimenting now and see that the complaints are justified) - dantelol
|
Try this function :
function file_get_contents_utf8($fn) { $content = file_get_contents($fn); return mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)); } - I also thought about mb_detect_encoding () but still this function doesn’t correctly define \ if to load in ASCII, it gives out something like this "like a û to [думаю думаю" I think I can specify a ready-made array of possible encodings of type as $ ary [ ] = "ASCII"; $ ary [] = "JIS"; $ ary [] = "EUC-JP"; mb_detect_encoding ($ str, $ ary); - dantelol
|