Reputation: 2311
I have a Windows program that is capable of handling UTF16 on input. On the PHP script I have encoding declared as UTF8. How do I call shell_exec or similar functions to launch the program and pass the parameter unchanged? Or is it not possible at all?
Upvotes: 3
Views: 843
Reputation: 536449
PHP implements shell_exec
using the C standard library function popen
. This is a byte-oriented function.
The Windows C runtime interprets byte data to stdlib functions as representing text encoded in the current code page, by default a locale-specific code page that will never be a UTF, so unfortunately you can't reliably get Unicode down that path.
You could try running the app with the code page changed to 65001, the Windows code page that should be UTF-8. However there are a number of stdlib bugs that make working in code page 65001 unreliable, so chances are it won't work. And if you're running in a web server, fiddling with process-global locale settings is a dicey prospect.
This is a problem with all tools that use C stdlib features, which is almost all scripting languages. Only reliable way to interact with Unicode in args or envvars, when you detect you're running under Windows, is to use the native Win32 API functions instead. On PHP it looks like you might be able to do this using w32api_invoke_function
to call CreateProcessW
. (Haven't done it myself, but the same strategy works with Python using ctypes
.)
Alternatively, pass data through the stdin/stdout streams. Then you can read them as bytes and do any Unicode conversions yourself manually.
Upvotes: 2