Add Str::escapeAttr()

This commit is contained in:
Giuseppe Criscione 2020-12-01 20:08:25 +01:00
parent 561649be2b
commit 001dfe2e51

View File

@ -86,13 +86,21 @@ class Str
}
/**
* Escape HTML tags from a given string
* Escape HTML tags, quotes and ampersands from a given string
*/
public static function escape(string $string): string
{
return htmlspecialchars($string, ENT_COMPAT | ENT_SUBSTITUTE, 'utf-8', false);
}
/**
* Escape quotes and ampersands from a given string (to be used with HTML attributes)
*/
public static function escapeAttr(string $string): string
{
return str_replace(['&lt;', '&gt;'], ['<', '>'], static::escape($string));
}
/**
* Remove HTML tags and entities from a given string
*/