The htmlspecialchars
function returns an empty string if the input string contains an invalid code unit sequence within the given encoding, unless either the ENT_IGNORE
or ENT_SUBSTITUTE
flags are set.
The ENT_IGNORE
flag silently discards invalid code unit sequences instead of returning an empty string. Using this flag is discouraged as it may have security implications.
The ENT_SUBSTITUTE
falg replaces invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string.
You could try to set one of these flags.
htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE);