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;
    }
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;
    }
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 ; }
Replace invalid XML characters.

@link http://www.phpwact.org/php/i18n/charsets#xml See utf8_for_xml() function
@link http://www.w3.org/TR/REC-xml/#charsets
@link https://github.com/mibe/FeedWriter/issues/30

@access   public
@param    string $string      string which should be filtered
@param    string $replacement replace invalid characters with this string
@return   string              the filtered string
Replace invalid XML characters .