note 93620 added to function.htmlspecialchars

From: Date: Fri, 18 Sep 2009 17:16:01 +0000
Subject: note 93620 added to function.htmlspecialchars
Groups: php.notes 
Request: Send a blank email to php-notes+get-160835@lists.php.net to get a copy of this message
This may seem obvious, but it caused me some frustration. If you try and use htmlspecialchars with the $charset argument set and the string you run it on is not actually the same charset you specify, you get any empty string returned without any notice/warning/error. <?php $ok_utf8 = "A valid UTF-8 string"; $bad_utf8 = "An invalid UTF-8 string"; var_dump(htmlspecialchars($bad_utf8, ENT_NOQUOTES, 'UTF-8')); // string(0) "" var_dump(htmlspecialchars($ok_utf8, ENT_NOQUOTES, 'UTF-8')); // string(20) "A valid UTF-8 string" ?> So make sure your charsets are consistent <?php $bad_utf8 = "An invalid UTF-8 string"; // make sure it's really UTF-8 $bad_utf8 = mb_convert_encoding($bad_utf8, 'UTF-8', mb_detect_encoding($bad_utf8)); var_dump(htmlspecialchars($bad_utf8, ENT_NOQUOTES, 'UTF-8')); // string(23) "An invalid UTF-8 string" ?> I had this problem because a Mac user was submitting posts copy/pasted from a program and it contained weird chars in it. ---- Server IP: 92.48.74.199 Probable Submitter: 86.145.140.2 ---- Manual Page -- http://www.php.net/manual/en/function.htmlspecialchars.php Edit -- https://master.php.net/note/edit/93620 Del: integrated -- https://master.php.net/note/delete/93620/integrated Del: useless -- https://master.php.net/note/delete/93620/useless Del: bad code -- https://master.php.net/note/delete/93620/bad+code Del: spam -- https://master.php.net/note/delete/93620/spam Del: non-english -- https://master.php.net/note/delete/93620/non-english Del: in docs -- https://master.php.net/note/delete/93620/in+docs Del: other reasons-- https://master.php.net/note/delete/93620 Reject -- https://master.php.net/note/reject/93620 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160835) next »