php urlencode是否与java urlencode相同?

时间:2012-04-17 07:39:11

标签: java php urlencode encode

在PHP中:

php -r "echo urlencode('["IM"]'); "  

结果为%5BIM%5D

但是在java

String text = URLEncoder.encode('["IM"]',"UTF-8");
System.out.println("text" + text);

结果为text%5B%22IM%22%5D

这两个功能有什么不同?如何实现java代码来完成php的相同功能?

3 个答案:

答案 0 :(得分:3)

String text = URLEncoder.encode('[IM]',"UTF-8");
System.out.println("text" + text);

给出%5BIM%5D

echo urlencode('["IM"]');

给出%5B%22IM%22%5D

echo urlencode('[IM]');

给出%5BIM%5D

echo urlencode('[\"IM\"]');

给出%5B%5C%22IM%5C%22%5D

答案 1 :(得分:1)

尝试逃避内部qoutes

php -r "echo urlencode('[\"IM\"]'); "

答案 2 :(得分:1)

php -r "echo urlencode('["IM"]'); "

结果是%5BIM%5D,因为函数urlencode实际上只是将字符串[IM]作为其输入。而在java

String text = URLEncoder.encode('["IM"]',"UTF-8");

传递的字符串实际上是:["IM"],它生成值%22,它是引号标记"的编码字符串。