note 77288 added to function.htmlspecialchars

From: Date: Thu, 23 Aug 2007 04:21:00 +0000
Subject: note 77288 added to function.htmlspecialchars
Groups: php.notes 
Request: Send a blank email to php-notes+get-129719@lists.php.net to get a copy of this message
:// Escapes strings to be included in javascript :function jsspecialchars($s) { : return preg_replace('/([^ :!#$%@()*+,-.\x30-\x5b\x5d-\x7e])/e', : "'\\x'.(ord('\\1')<16? '0': '').dechex(ord('\\1'))",$s); :} This function DOES NOT produce correct output in PHP5. Any strings containing a ” will be improperly escaped to \x5c, when it should be \x22. I am not very good with regular expressions, so this is my solution to the problem. //this is a workaround for jsspecialchars! function ord2($s) { if (strlen($s) == 2) { return ord(substr($s,1,1)); } else { return ord($s); } } function JS_SpecialChars($s) { return preg_replace(’/([^ !#$%@()*+,.\x30\x5b\x5d-\x7e])/e’, ”’\\x’.(ord2(’\\1’)&lt;16? ‘0’: ’’).dechex(ord2(’\\1’))”,$s); } I am sure that there is a better solution, but I can’t figure one out. This approach will probably also fix any other characters that end up being improperly escaped. ---- Server IP: 69.147.83.197 Probable Submitter: 71.176.212.105 ---- Manual Page -- http://www.php.net/manual/en/function.htmlspecialchars.php Edit -- https://master.php.net/note/edit/77288 Del: integrated -- https://master.php.net/note/delete/77288/integrated Del: useless -- https://master.php.net/note/delete/77288/useless Del: bad code -- https://master.php.net/note/delete/77288/bad+code Del: spam -- https://master.php.net/note/delete/77288/spam Del: non-english -- https://master.php.net/note/delete/77288/non-english Del: in docs -- https://master.php.net/note/delete/77288/in+docs Del: other reasons-- https://master.php.net/note/delete/77288 Reject -- https://master.php.net/note/reject/77288 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129719) next »