Skip to content

Instantly share code, notes, and snippets.

@seutje
Created January 15, 2016 14:34
Show Gist options
  • Save seutje/1c0c2ec89f467c893422 to your computer and use it in GitHub Desktop.
Save seutje/1c0c2ec89f467c893422 to your computer and use it in GitHub Desktop.
PHP helper function to escape javascript strings like a true paranoid mofo
<?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;
}
@seutje
Copy link
Author

seutje commented Jan 18, 2016

poops all ovre UTF-8 :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment