From 816bb7c9ee15ae49dcada20bf1f0b8034c839521 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 28 Feb 2025 10:19:36 +0900 Subject: [PATCH] Allow encoding ovrride for htmlentities --- www/lib/CoreLibs/Convert/Html.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/www/lib/CoreLibs/Convert/Html.php b/www/lib/CoreLibs/Convert/Html.php index 2094dc55..ae057662 100644 --- a/www/lib/CoreLibs/Convert/Html.php +++ b/www/lib/CoreLibs/Convert/Html.php @@ -10,9 +10,16 @@ namespace CoreLibs\Convert; class Html { + /** @var int */ public const SELECTED = 0; + /** @var int */ public const CHECKED = 1; + // TODO: check for not valid htmlentites encoding + // as of PHP 8.4: https://www.php.net/manual/en/function.htmlentities.php + /** @#var array */ + // public const VALID_HTMLENT_ENCODINGS = []; + /** * full wrapper for html entities * @@ -22,14 +29,19 @@ class Html * encodes in UTF-8 * does not double encode * - * @param mixed $string string to html encode - * @param int $flags [default: ENT_QUOTES | ENT_HTML5] + * @param mixed $string string to html encode + * @param int $flags [default=ENT_QUOTES | ENT_HTML5] + * @param string $encoding [default=UTF-8] * @return mixed if string, encoded, else as is (eg null) */ - public static function htmlent(mixed $string, int $flags = ENT_QUOTES | ENT_HTML5): mixed - { + public static function htmlent( + mixed $string, + int $flags = ENT_QUOTES | ENT_HTML5, + string $encoding = 'UTF-8' + ): mixed { if (is_string($string)) { - return htmlentities($string, $flags, 'UTF-8', false); + // if not a valid encoding this will throw a warning and use UTF-8 + return htmlentities($string, $flags, $encoding, false); } return $string; } @@ -37,7 +49,7 @@ class Html /** * strips out all line breaks or replaced with given string * @param string $string string - * @param string $replace replace character, default ' ' + * @param string $replace [default=' '] replace character * @return string cleaned string without any line breaks */ public static function removeLB(string $string, string $replace = ' '): string