unescape文本在php中编码,在javascript中[重复]

This question already has an answer here:

I have some server side code in PHP which looks like this

$var2 = htmlspecialchars($var1);

For example,

"It's Monday" gets converted to "It's Monday"

I am not able to decode the text back in javascript using the function unescape.

unescape("It's Monday") // returns "It's Monday"

How can i decode this in javascript to get the original string back, apart from using replace (because this way i'd have to manually handle all special cases) ?

</div>

You can assign the html-encoded text to an dummy element and get back it's text, to do this:

$('<span/>').html("It&#039;s Monday").text()

returns "It's Monday"

Note: The above does not modify the DOM (unlike I said before I edited the answer)

You'll have to encode your text using urlencode in PHP if you want to decode it again using JavaScript's unescape.

// PHP
urlencode("It's Monday") // returns "It%27s%20Monday"

// JavaScript
unescape("It%27s%20Monday") // returns "It's Monday"