Created
January 15, 2016 14:34
-
-
Save seutje/1c0c2ec89f467c893422 to your computer and use it in GitHub Desktop.
PHP helper function to escape javascript strings like a true paranoid mofo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Extremely paranoid escaping function for use with inline JS strings. | |
* | |
* Escapes every single character to a \x# format. | |
* | |
* @param string | |
* | |
* @return string | |
*/ | |
function paranoid_js_escape($str) { | |
$output = ''; | |
$length = strlen($str); | |
for($i = 0; $i < $length; $i++) { | |
$output .= '\\x' . sprintf('%02x', ord(substr($str, $i, 1))); | |
} | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
poops all ovre UTF-8 :(