user3588722
user3588722

Reputation: 5

PHP | Polish characters - convert UTF-8 to ANSI

I was trying to converting string UTF-8 to ANSI (Windows-1252).

Here is some examples of my codes:

https://3v4l.org/MDcIj

Non of them doesn't work and polish characters has question marks or diamonts :/

The only solution for now is this library: CkCharset Converter but i don't want to use such a big library for saving one file.

I'll be grateful for any help with this problem.

The file with script has header like and encoding UTF-8:

header('Content-Type: text/html; charset=utf-8');

Upvotes: 0

Views: 8749

Answers (3)

user3588722
user3588722

Reputation: 5

Thank You all for help.

iconv('UTF-8', 'Windows-1250//IGNORE', $result)

Without //IGNORE parametr function returning notice error

Notice: iconv(): Detected an illegal character in input string

but it's works just fine :)

Upvotes: 0

Alex
Alex

Reputation: 4811

I think that you should transliterate the string: replace non-latin characters with the corresponding latin-ascii characters. As far as I know, the most reliable solution is about the usage of \Transliterator from module intl.

It works for wide range of languages, including, for example, Polish, Cyrillic or Chinese symbols. I would like to remind you, that the manual replacement of polish symbols will fail for other languages.

I think that this code should work for you:

$rule = ':: Any-Latin; :: Latin-ASCII; :: NFD; :: [:Nonspacing Mark:] Remove; :: Lower(); :: NFC;';

$latinString = \Transliterator::createFromRules($rule, \Transliterator::FORWARD)
        ->transliterate($maybePolishString);

You can find more info on INTL documentation page: https://secure.php.net/manual/en/book.intl.php

Upvotes: 2

Joni
Joni

Reputation: 111389

If by ANSI you mean the original ASCII encoding that only supports the English letters A-Z, you can use iconv with the translit option:

php > $txt = "[ 'ą', 'ć', 'ę', 'ł', 'ń', 'ó', 'ś', 'ż' , 'ź', 'Ą', 'Ć', 'Ę', 'Ł', 'Ń', 'Ó', 'Ś', 'Ż', 'Ź']";
php > echo iconv('utf8', 'ascii//translit', $txt);
[ 'a', 'c', 'e', 'l', 'n', 'o', 's', 'z' , 'z', 'A', 'C', 'E', 'L', 'N', 'O', 'S', 'Z', 'Z']

If instead you mean Windows code page 1250, use:

iconv('utf8', 'windows-1250', $txt);

If instead you mean DOS code page 852, use:

iconv('utf8', 'cp852', $txt);

You should check which encoding your billing program expects. If it's a DOS program from the 80's, it's probably cp852, but if it's a Windows program from the 90's it's probably windows-1250.

Upvotes: 0

Related Questions