Try to sanitize UTF8, stripping invalid byte sequences. Not perfect. Does not surrogate characters.

@param string $value
@return string
345996
public static function SanitizeUTF8($value)
    {
        if (self::getIsIconvEnabled()) {
            $value = @iconv('UTF-8', 'UTF-8', $value);
            return $value;
        }

        if (self::getIsMbstringEnabled()) {
            $value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
            return $value;
        }

        // else, no conversion
        return $value;
    }
592325
public static function ascii($value)
    {
        foreach (static::charsArray() as $key => $val) {
            $value = str_replace($val, $key, $value);
        }

        return preg_replace('/[^\x20-\x7E]/u', '', $value);
    }
597006
public static function dumpString(string $value): string
    {
        if ('' === $value) {
            return "''";
        }

        // non UTF-8 string will encode "\xFF" and require <">
        if (!self::isUtf8String($value)) {
            return '"' . \preg_replace_callback(
                // ---------------------------------------------------
                //     0x80 ..    0x7FF => C2 80       ... DF BF
                // ---------------------------------------------------
                //    0x800 ..    0xFFF => E0 A0 80    ... E0 BF BF
                //   0x1000 ..   0xFFFF => E1 80 80    ... EF BF BF
                // ---------------------------------------------------
                //  0x10000 ..  0x3FFFF => F0 90 80 80 ... F0 BF BF BF
                //  0x40000 ..  0xFFFFF => F1 80 80 80 ... F3 BF BF BF
                // 0x100000 .. 0x10FFFF => F4 80 80 80 ... F4 8F BF BF

                // " \x22
                // $ \x24
                // \ \x5C
                '/
                    \\G
                    (?:
                        [\\x20\\x21\\x23\\x25-\\x5B\\x5D-\\x7E]++
                        | [\\xC2-\\xDF] [\\x80-\\xBF]
                        | \\xE0 [\\xA0-\\xBF] [\\x80-\\xBF]
                        | [\\xE1-\\xEF] [\\x80-\\xBF]{2}
                        | \\xF0 [\\x90-\\xBF] [\\x80-\\xBF]{2}
                        | [\\xF1-\\xF3] [\\x80-\\xBF]{3}
                        | \\xF4 [\\x80-\\x8F] [\\x80-\\xBF]{2}
                    )*+
                    \\K
                    (?:
                        ( ["$\\\\] )
                        |
                        ( . )
                    )
                /xs',
                /** @uses _escapeStringCallback() */
                \Closure::fromCallable([__CLASS__, '_escapeStringCallback']),
                $value
            ) . '"';
        }

        // valid UTF-8 string with control ASCII characters
        // also will encode "\xFF" and require <">, but regexp is much simple
        if (\preg_match('/[\\x00-\\x1F\\x7F]/', $value)) {
            return '"' . \preg_replace_callback(
                '/(["$\\\\])|([\\x00-\\x1F\\x7F])/',
                /** @uses _escapeStringCallback() */
                \Closure::fromCallable([__CLASS__, '_escapeStringCallback']),
                $value
            ) . '"';
        }

        return \var_export($value, true);
    }
880537
public function sanitize($value)
    {
        //remove any quotation and replace with whitespace
        $value = preg_replace('/\"/', '', $value);

        //remove any '-' from the string they will be used as concatonater
        $value = str_replace($this->_separator, ' ', $value);

        //convert to ascii characters
        $value = $this->getObject('lib:filter.ascii')->sanitize($value);

        //lowercase and trim
        $value = trim(strtolower($value));

        //remove any duplicate whitespace, and ensure all characters are alphanumeric
        $value = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array($this->_separator,''), $value);

        //remove repeated occurrences of the separator
        $value = preg_replace('/['.preg_quote($this->_separator, '/').']+/', $this->_separator, $value);

        //limit length
        if (strlen($value) > $this->_length) {
            $value = substr($value, 0, $this->_length);
        }

        //remove unwanted separators from the ends
        $value = trim($value, $this->_separator);

        return $value;
    }
1009225
public function sanitize($value)
    {
        $value = JApplicationHelper::stringURLSafe($value);

        //limit length
        if (mb_strlen($value) > $this->_length) {
            $value = mb_substr($value, 0, $this->_length);
        }

        return $value;
    }
827080
private function formatValue($value)
    {
        if ($value === '') {
            return '';
        }

        if ($value === null) {
            return '<comment>Null</comment>';
        }

        if (is_bool($value)) {
            return '<comment>' . ($value ? 'True' : 'False') . '</comment>';
        }

        if (empty($value)) {
            return '<comment>Empty</comment>';
        }

        if (is_array($value)) {
            return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        }

        if (is_object($value)) {
            return sprintf('<%s>', get_class($value));
        }

        if (is_scalar($value)) {
            return $value;
        }

        throw new InvalidArgumentException(sprintf('Do not know how to format value "%s"', print_r($value, true)));
    }
1225503
public function filter($value)
    {
        if ($this->encodingFormat !== null) {
            return mb_strtoupper($value, $this->encodingFormat);
        }

        return mb_strtoupper($value);
    }
985683
protected function _sanitize($value)
    {
        $string = htmlentities(utf8_decode($value));
        $string = preg_replace(
            array('/&szlig;/','/&(..)lig;/', '/&([aouAOU])uml;/','/&(.)[^;]*;/'),
            array('ss',"$1","$1".'e',"$1"),
            $string
        );

        return $string;
    }
747878
public static function sanitizeCategoryName($value)
    {
        $output = preg_replace('/\s+/', '-', trim($value));
        $output = preg_replace('/[^a-z0-9\-]/', '',  strtolower($output));
        $output = preg_replace('/^[0-9\-_]+/', '',  strtolower($output));
        return substr($output, 0, 32);
    }
1048818
public function utf8Value($value)
    {
        if (is_array($value)) {
            return $this->utf8AllTheThings($value);
        }

        return StringHelper::convertToUtf8($value);
    }
594738
public function escapeSpecialChars($value)
    {
        if (is_object($value)) {
            return $value;
        }

        if (is_array($value)) {
            $value = $this->escapeArraySpecialChars($value);
        } elseif (is_string($value)) {
            $value = $this->escapeStringSpecialChars($value);
        }

        return $value;
    }
1147270
protected function _sanitize($value)
    {
        // If instance of AnConfig casting to string will make it encode itself to JSON
        if ($value instanceof AnConfig) {
            $result = (string) $value;
        } else {
            //Don't re-encode if the value is already in json format
            if (is_string($value) && (json_decode($value) !== null)) {
                $result = $value;
            } else {
                $result = json_encode($value);
            }
        }

        return $result;
    }
563032
public function filter($value)
    {
        if ($this->encodingFormat !== null) {
            return mb_substr($value, $this->start, $this->length, $this->encodingFormat);
        }

        return mb_substr($value, $this->start, $this->length);
    }
63614
public function sanitize($value)
    {
        // Escape UTF-8 characters
        $value = strtr(rawurlencode($value), static::$_special_characters);

        return filter_var($value, FILTER_SANITIZE_URL);
    }
378399
public function sanitize($value)
    {
        if (null === $this->_charList) {
            return trim((string) $value);
        } else {
            return trim((string) $value, $this->_charList);
        }
    }
652203
private static function sanitizeString($value)
    {
        // $_GET and $_REQUEST already urldecode()'d
        // decode
        // note: before php 5.2.7, htmlspecialchars() double encodes &#x hex items
        $value = html_entity_decode($value, self::HTML_ENCODING_QUOTE_STYLE, 'UTF-8');

        $value = self::sanitizeNullBytes($value);

        // escape
        $tmp = @htmlspecialchars($value, self::HTML_ENCODING_QUOTE_STYLE, 'UTF-8');

        // note: php 5.2.5 and above, htmlspecialchars is destructive if input is not UTF-8
        if ($value != '' && $tmp == '') {
            // convert and escape
            $value = utf8_encode($value);
            $tmp = htmlspecialchars($value, self::HTML_ENCODING_QUOTE_STYLE, 'UTF-8');
            return $tmp;
        }
        return $tmp;
    }
446933
public function sanitize_installed_version( $value ) {
		$value = trim( (string) $value );

		if ( (bool) preg_match( '/[^\d\.]/', $value ) ) {
			add_settings_error(
				'soter_installed_version',
				'invalid_soter_installed_version',
				'Installed version can only contain the following characters: 0123456789.'
			);

			return $this->installed_version;
		}

		return $value;
	}