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 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.

Dealing with UTF-8 text using the PHP mail()-function

On one of my PHP-based websites, users can send mails via a simple text form. I had a problem with special characters in the subject or message body. The webpage has a <meta charset=”utf-8″ />-tag, which causes problems when people enter special characters, like é or € or “smart quotes”. They get sent via the mail()-function and show up like Ã³ or Ã© or â€™.

Deep down in the user comments in the documentation on the mail()-function, I found a small function that solved this easily:

function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') {
 $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
 $result = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header);
 return $result;
}

Just use mail_utf8() instead of mail() on a UTF-8 page and a proper UTF-8 mail will be sent out.