简体   繁体   中英

Best way to encode special chars in url

What is the best way to encode special characters in a url? Let's say I pass an variable from javascript to a php script that way:

http://example.com/my sp3c!al var!a$le

What is the best way to encode that special characters (like whitespace, !, $, /, \\ etc.)? Is there a method in javascript to encode it with a corresponding function in php to decode it there?

您需要使用encodeURIComponent(yourvar);

You can do it in three ways

encodeURI(yourvar) output http://example.com/my%20sp3c!al%20var!a$le

amd

encodeURIComponent(yourvar)

output http%3A%2F%2Fexample.com%2Fmy%20sp3c!al%20var!a%24le

escape()

output http%3A//example.com/my%20sp3c%21al%20var%21a%24le

using escape is not recomended because it is deprecated since ECMAScript v3.

functions is for JavaScript escaping, not HTTP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM