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:

  1. Give up and process everything in UTF/multibyte
  2. Use preg_match() (as with any regex matching in every other language I assume it always slower then native string/character tools)
  3. Use some clever mechanism to manage locales/charsets?
Asked on July 16, 2020 in php.
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.