问题
I am trying to make a dir using a php in english and it's working. But when i try in hebrew, it creats the folder in gibrish like so: ׳”׳•׳¨׳“׳”| ׳׳©׳—׳§ ׳©׳•׳׳˜
how can i fix it? thanks
回答1:
PHP has some Unicode support when dealing with strings, but internally PHP's string type is single-byte and does not support multibyte encodings. If you save your PHP file in UTF-8 format, you can output strings in UTF-8 with the proper HTTP encodings, but many of PHP's functions don't support multi-byte strings. There are some that do, but still most that do not.
I looked around in the PHP source and most of the filesystem functions like mkdir
are not Unicode aware. They operate on single byte strings which is why you get the ASCII gibberish when you try to create a directory.
You might try calling setlocale
and setting it to he_IL
before calling mkdir
to see if this makes any difference.
For more information:
A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support. See details of the string type.
The details talk more about different encodings, functions that are multi-byte aware and different behavior based on whether or not Zend Multibyte support is enabled in PHP and how saving your files in different encodings effects strings within PHP.
Unfortunately at this time, I don't believe you will be able to create a directory containing multi-byte strings in PHP directly.
Internally, the function mkdir
calls this function mkdir in file.c which calls _php_stream_mkdir which uses the plain files wrapper
to call php_mkdir_ex to ultimately create the directory using virtual_mkdir that uses the C mkdir function to create the directory.
I think by the time it calls mkdir to create the directory on the file-system, all encodings have been lost and it is treated as a single-byte char * string.
回答2:
Use mb_convert_encoding to convert the name (string) from UTF-8 to ISO-8859-8. I've tested it with PHP 7 and Windows 10.
回答3:
Basically you cannot unless your OS's encoding system supports these characters. Hebrew also must be part of the extended UTF-8 package right?
For example unde Windows the default codepage is iso-8859-1 which doesn't have hebrew.
I think switching it to UTF-8 might work as now new Windows version allow all Unicode characters in file names, but its not really recommended
来源:https://stackoverflow.com/questions/11703711/php-mkdir-in-hebrew-creates-gibrish-folder