Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htmlspecialchars(): Invalid multibyte sequence in argument

I am getting this error in my local site.

Warning (2): htmlspecialchars(): Invalid multibyte sequence in argument in [/var/www/html/cake/basics.php, line 207]

Does anyone knows, what is the problem or what should be the solution for this?

Thanks.

like image 401
gautamlakum Avatar asked Sep 27 '10 12:09

gautamlakum


3 Answers

Be sure to specify the encoding to UTF-8 if your files are encoded as such:

htmlspecialchars($str, ENT_COMPAT, 'UTF-8');

The default charset for htmlspecialchars is ISO-8859-1 (as of PHP v5.4 the default charset was turned to 'UTF-8'), which might explain why things go haywire when it meets multibyte characters.

like image 192
Tatu Ulmanen Avatar answered Oct 16 '22 10:10

Tatu Ulmanen


I ran in to this error on production and found this great post about it -

http://insomanic.me.uk/post/191397106/php-htmlspecialchars-htmlentities-invalid

It appears to be a bug in PHP (for CentOS at least) that displays this error on when display errors is Off!

like image 41
gingerCodeNinja Avatar answered Oct 16 '22 09:10

gingerCodeNinja


You are feeding corrupted character data into the function, or not specifying the right encoding.

I had this issue a while ago, old behavior (prior to PHP 5.2.7 I believe) was to return the string despite corruption, but since that version it will throw this error instead.

My solution involved writing a script to feed my strings through iconv using the //IGNORE modifier to remove corrupted data.

(We had a corrupted database which had some strings in UTF-8, some in latin-1 usually with incorrectly defined character types on the columns).

(Looking at the comment to Tatu's answer, I would start by looking at (and playing with) the contents of the $charset variable.

like image 4
berty Avatar answered Oct 16 '22 09:10

berty