How do you narrow locale/codepage context in modern PHP?
I have php_mod
7.2.31 running under LightSpeed, the environment locale is UTF8. My very tiny test project only need to work with ASCII data, however, because env. locale is UTF8, ctype_alnum()
check is skewed now. I could resolve it by call to setlocale()
, however, according to the docs setlocale()
is unsafe in threaded environment:
Warning The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS, HHVM or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale().
I guess this makes sense. Hosting is managed by my provider, so I dont know all the details about it but for safety sake I’m assuming it is multithreaded. How do people handle such cases? My guesses so far:
- Give up and process everything in UTF/multibyte
- Use preg_match() (as with any regex matching in every other language I assume it always slower then native string/character tools)
- Use some clever mechanism to manage locales/charsets?