How to ignore @ errors in a custom PHP error handler

In my PHP pages, I’m using a custom error handler, to make potential errors a bit more user-friendly. However, in some very particular cases, I don’t want an error to be raised (e.g. I’m logging all errors to a server file, but if writing to that file does not work for whatever reason, I don’t want to bother the user with it). In those cases, I’m using the @ error-control operator to ignore any error:
@error_log($error_trace.' ['.str_replace("\n",'\n',$error_message).'] ['.str_replace("\n",'\n',$error_data)."]\n",3,ERROR_LOG);

The @ error-control operator, However, even with @, the custom error handler is still triggered.

Thanks to this blog post I learned that error_reporting is 0 if the statement that caused the error was prepended by the @ error-control operator. So the solution is to add:
if (error_reporting() === 0) {
// continue script execution, skipping standard PHP error handler  (e.g.when using @ in front of an expression)
return true;
}

to the custom error handler

PHP setlocale depends on underlying system (XAMPP on Windows)

I’m maintaining a few websites, so I have a local webserver running to do testing and debugging. Until recently, I was using IIS on my Windows laptop. However, I ditched IIS a while ago, replacing it with XAMPP. This is much closer to the environment that’s running the websites (e.g. I can use .htaccess directly, rather than converting it to a web.config file).

This all worked fine, except for setlocale in PHP. I wanted to use Dutch (Belgian), but I could not get
setlocale(LC_ALL, 'nl_BE');
to work on my local machine.

I searched the web, but did not find any useful information. So I looked at the PHP setlocale helpinfo again, and noticed the Tip in the NotesWindows users will find useful information about locale strings at Microsoft’s MSDN website.

After checking, it turned out that Microsoft decided to go for another “standard”, so ‘nl_BE’ was not recognized. To use Dutch on Windows, I needed
setlocale(LC_ALL, 'nld_nld');

Oh well…

PHP function to convert only words in UPPERCASE to Startcase

I needed a function in PHP that would convert words in uppercase to Startcase, i.e. the first letter of the word in uppercase and the rest in lowercase. A combination of ucwords with strtolower can do that, however, I only wanted the words that were fully in uppercase to be converted, not the other words.

To give a example, I wanted
SHINKANSEN SAKURA 555 naar Hiroshima (treinpas valideren in Shinagawa station)
to be converted to
Shinkansen Sakura 555 naar Hiroshima (treinpas valideren in Shinagawa station)
(and not to)
Shinkansen Sakura 555 Naar Hiroshima (treinpas Valideren In Shinagawa Station)

You can do that relatively easily with preg_replace_callback:

function uc_uppercase_words($string) {
  $result = preg_replace_callback(
    '|\b[A-Z]{2,}\b|',
    function ($matches) {
      return ucwords(strtolower($matches[0]));
    },$string);
  return $result;
}

This replaces any word of least 2 capital letters to a word with only the first letter capitalized.