public static function filterInvalidXMLChars($string, $replacement = '_') // default to '\x{FFFD}' ???
    {
        $result = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/u', $replacement, $string);

        // Did the PCRE replace failed because of bad UTF-8 data?
        // If yes, try a non-multibyte regex and without the UTF-8 mode enabled.
        if ($result == NULL && preg_last_error() == PREG_BAD_UTF8_ERROR)
            $result = preg_replace('/[^\x09\x0a\x0d\x20-\xFF]+/', $replacement, $string);

        // In case the regex replacing failed completely, return the whole unfiltered string.
        if ($result == NULL)
            $result = $string;

        return $result;
    }