Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mbstring class error on convEncoding method #3

Open
imansa opened this issue Aug 21, 2024 · 14 comments
Open

Mbstring class error on convEncoding method #3

imansa opened this issue Aug 21, 2024 · 14 comments
Labels
need more info Need more information to address it

Comments

@imansa
Copy link

imansa commented Aug 21, 2024

method return type is string and return value of calling iconv method returned directly but iconv method can send false return sometimes and create a Fatal Error

protected static function convEncoding(string $str, string $from, string $to): string

need change

sample data

'1H*Ì*1', 'UTF-8', 'UTF-32'

@jfcherng
Copy link
Owner

Does iconv always return false or it only returns false for your sample data?

@imansa
Copy link
Author

imansa commented Aug 21, 2024

According to php documention iconv Returns the converted string, or false on failure.

in my tests returning false is not for all datas

@jfcherng
Copy link
Owner

jfcherng commented Aug 21, 2024

mostly likely the input string is out of ASCII region and not utf-8, otherwise I see no reason a utf-8 valid string failed to be converted into utf-32.

@jfcherng
Copy link
Owner

jfcherng commented Aug 21, 2024

Just to make sure,

$ php -r "var_dump(iconv('UTF-8', 'UTF-32', '1H*Ì*1'));"

shows false on your side?


$ php -r "var_dump(iconv('UTF-8', 'UTF-32', 'Ì'));"

shows false too?

@imansa
Copy link
Author

imansa commented Aug 24, 2024

i couldn't detected data that result false returning for iconv but this hapend multiple times in production. when iconv can return false i think it's better to handle failure state for using it

@jfcherng
Copy link
Owner

jfcherng commented Aug 24, 2024

do you have php mbstring extension installed?

@jfcherng
Copy link
Owner

Just to make sure,

$ php -r "var_dump(iconv('UTF-8', 'UTF-32', '1H*Ì*1'));"

shows false on your side?

$ php -r "var_dump(iconv('UTF-8', 'UTF-32', 'Ì'));"

shows false too?

i couldn't detected data that result false returning for iconv

you don't have to. you just have to execute those 2 cli commands and tell me the returned data in the console.

@imansa
Copy link
Author

imansa commented Aug 24, 2024

bash-4.4$ php -r "var_dump(iconv('UTF-8', 'UTF-32', '1H*Ì*1'));"
string(28) "��1H*�*1"
bash-4.4$ php -r "var_dump(iconv('UTF-8', 'UTF-32', 'Ì'));"
string(8) "���"

@imansa
Copy link
Author

imansa commented Aug 24, 2024

PHP Version 8.1.29
System 	Linux admin 4.18.0-513.24.1.el8_9.x86_64 #1 SMP Thu Apr 4 18:13:02 UTC 2024 x86_64 


mbstring
Multibyte Support 	enabled
Multibyte string engine 	libmbfl
HTTP input encoding translation 	disabled
libmbfl version 	1.3.2 

@jfcherng
Copy link
Owner

jfcherng commented Aug 24, 2024

bash-4.4$ php -r "var_dump(iconv('UTF-8', 'UTF-32', '1H*Ì*1'));"
string(28) "��1H*�*1"
bash-4.4$ php -r "var_dump(iconv('UTF-8', 'UTF-32', 'Ì'));"
string(8) "���"

Hmm... looks perfect to me. So I would assume the issue is that some of your UTF source code files are not UTF-8 encoded.

bash-4.4$ php -r "var_dump(iconv('UTF-8', 'UTF-32', '1HÌ1'));"
string(28) "��1H1"

As you can see, your example data can't reproduce the issue from the console.

@jfcherng
Copy link
Owner

jfcherng commented Aug 24, 2024

You can try to use mbstring by default to see whether it works. Just change the order of these two if blocks:

protected static function detectConvEncoding(): int
{
if (\function_exists('iconv') && iconv('UTF-8', 'UTF-32', 'A') !== false) {
return static::MBSTRING_CONVMETHOD_ICONV;
}
if (\function_exists('mb_convert_encoding') && mb_convert_encoding('A', 'UTF-32', 'UTF-8') !== false) {
return static::MBSTRING_CONVMETHOD_MBSTRING;
}
throw new \RuntimeException('Either "iconv" or "mbstring" extension is required.');
}

@imansa
Copy link
Author

imansa commented Aug 24, 2024

i have no problem to solve this problem but when i am using composer to update dependencies, all changes will be replaced. i created this issue to fix bug for returning false state when using iconv

@jfcherng
Copy link
Owner

jfcherng commented Aug 24, 2024

Without your experiments, no one knows how to fix it because obviously I am not affected. And you are the first one who is affected since this lib has been created, at least from the issue list here.

@jfcherng
Copy link
Owner

i have no problem to solve this problem

What you have changed on your side?

@jfcherng jfcherng added the need more info Need more information to address it label Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need more info Need more information to address it
Projects
None yet
Development

No branches or pull requests

2 participants