This is how I replaced the FILTER_SANITIZE_STRING constant. This way you can also use flags.
I had some PHP backend tests, and they work fine with this method.
Improvements are appreciated.
/**
* @param string $value
* @param array $flags
* @return string
*/
private static function sanitizeFilterString($value, array $flags): string
{
$noQuotes = in_array(FILTER_FLAG_NO_ENCODE_QUOTES, $flags);
$options = ($noQuotes ? ENT_NOQUOTES : ENT_QUOTES) | ENT_SUBSTITUTE;
$optionsDecode = ($noQuotes ? ENT_QUOTES : ENT_NOQUOTES) | ENT_SUBSTITUTE;
// Strip the tags
$value = strip_tags($value);
// Run the replacement for FILTER_SANITIZE_STRING
$value = htmlspecialchars($value, $options);
// Fix that HTML entities are converted to entity numbers instead of entity name (e.g. ' -> " and not ' -> "e;)
// https://stackoverflow.com/questions/64083440/use-php-htmlentities-to-convert-special-characters-to-their-entity-number-rather
$value = str_replace([""", "'"], [""", "'"], $value);
// Decode all entities
return html_entity_decode($value, $optionsDecode);
}