Rails-like escaping javascript with PHP
I'm trying to write the equivalent of Rails escape_javascript() in php with a preg_match() Is there any php built in function ? Can someone help ?
http://api.rubyonrails.org/classes/Actio开发者_开发知识库nView/Helpers/JavaScriptHelper.html#method-i-escape_javascript
# File actionpack/lib/action_view/helpers/javascript_helper.rb, line 50
def escape_javascript(javascript)
if javascript
javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }
else
''
end
end
Thanks !
json_encode()
I can't say that it's a one-to-one match, but it's a straightforward way to escape JavaScript values in PHP.
精彩评论