How does JS escape work in PHP?
The following function can be correctly parsed. There are many functions of Unescape on the Internet, but it is not easy to use.
This is one collected a long time ago. I don't know who wrote it, but there is no problem after testing ~
JavaScript code
Code
01. function phpUnescape ($ escstr)
02 .{
03. preg_match_all ("/% u [0-9A-Za-z] {4} | %. {2} | [0-9a-zA-Z. +-_] +/", $ escstr, $ matches );
04. $ ar = & $ matches [0];
05. $ c = "";
06. foreach ($ ar as $ val)
07 .{
08. if (substr ($ val, 0, 1 )! = "% ")
09 .{
10. $ c. = $ val;
11.} elseif (substr ($ val, 1, 1 )! = "U ")
12 .{
13. $ x = hexdec (substr ($ val, 1, 2 ));
14. $ c. = chr ($ x );
15 .}
16. else
17 .{
18. $ val = intval (substr ($ val, 2), 16 );
19. if ($ val <0x7F) // else -007f
20 .{
21. $ c. = chr ($ val );
22.} elseif ($ val <0x800) // 0080-0800
23 .{
24. $ c. = chr (0xC0 | ($ val/64 ));
25. $ c. = chr (0x80 | ($ val % 64 ));
26 .}
27. else // 0800-FFFF
28 .{
29. $ c. = chr (0xE0 | ($ val/64)/64 ));
30. $ c. = chr (0x80 | ($ val/64) % 64 ));
31. $ C. = CHR (0x80 | ($ Val % 64 ));
32 .}
33 .}
34 .}
35.
36. Return $ C;
37 .}
After escape encoding:
%u6D4B%u8BD5www.koyoz.com%22%22%27%27%3C%3E%26%26
After decoding:
Test www.koyoz.com "" ''<> &&
From: http://www.koyoz.com/blog? Action = show & id = 207