Re: [PHP] Escaping JavaScript strings

2008-05-29 Thread Richard Heyes

 ...

This is a PHP function that escapes strings so you can output them as a 
JS string. IIRC it assumes you're using single quotes to enclose your 
strings.


/**
* Function to appropriately escape a string so it can be output
* into javascript code.
*
* @param  string $string Input string to escape
* @return string Escaped string
*/
function escapeString($string)
{
$js_escape = array(\r = '\r',
   \n = '\n',
   \t = '\t',
   '  = \\',
   '\\' = '');

return strtr($string, $js_escape);
}

--
  Richard Heyes

 In Cambridge? Employ me
http://www.phpguru.org/cv

++
| Access SSH with a Windows mapped drive |
|http://www.phpguru.org/sftpdrive|
++

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Escaping JavaScript strings

2008-05-29 Thread Edward Kay

 
 This is a PHP function that escapes strings so you can output them as a 
 JS string. IIRC it assumes you're using single quotes to enclose your 
 strings.
 
  /**
  * Function to appropriately escape a string so it can be output
  * into javascript code.
  *
  * @param  string $string Input string to escape
  * @return string Escaped string
  */
  function escapeString($string)
  {
  $js_escape = array(\r = '\r',
 \n = '\n',
 \t = '\t',
 '  = \\',
 '\\' = '');
 
  return strtr($string, $js_escape);
  }


Just what was needed - thanks Richard.

Edward

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php