Skip to content

Commit

Permalink
MDL-8028 Add separate addslashes_js function for javascript quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Dec 30, 2006
1 parent 37edce7 commit 0d1cd0e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/weblib.php
Expand Up @@ -135,6 +135,28 @@ function p($var, $strip=false) {
echo s($var, $strip);
}

/**
* Does proper javascript quoting.
* Do not use addslashes anymore, because it does not work when magic_quotes_sybase is enabled.
*
* @param mixed value
* @return mixed quoted result
*/
function addslashes_js($var) {
if (is_string($var)) {
$var = str_replace('\\', '\\\\', $var);
$var = str_replace(array('\'', '"', "\n", "\r", "\0"), array('\\\'', '\\"', '\\n', '\\r', '\\0'), $var);
} else if (is_array($var)) {
$var = array_map('addslashes_js', $var);
} else if (is_object($var)) {
$a = get_object_vars($var);
foreach ($a as $key=>$value) {
$a[$key] = addslashes_js($value);
}
$var = (object)$a;
}
return $var;
}

/**
* Remove query string from url
Expand Down

0 comments on commit 0d1cd0e

Please sign in to comment.