How to get ¤ to display literally, not as an

2019-01-09 17:34发布

问题:

I'm using php to look at an XML file that has a URL in it. The URLs look something like this:

https://site.com/bacon_report?Id=1&report=1&currentDimension=2&param=1

When I echo out the URLs, the "&curren" shows up as "¤" (AKA #164, A4 or currency symbol) and the links don't work. This happens even though there isn't a closing semicolon for it. What is the cleanest way to make "&curren" display literally?

回答1:

Use the php function urlencode:

urlencode("https://site.com/bacon_report?Id=1&report=1&currentDimension=2&param=1"

will output

https%3A%2F%2Fsite.com%2Fbacon_report%3FId%3D1%26report%3D1%26currentDimension%3D2%26param%3D1 


回答2:

Funny enough I ran into the same problem just now and I found this answer. However, I found another solution which might even be better!

Simply put the variable at the beginning of your query string, and you will avoid the &curren completely.

Do:

https://site.com/bacon_report?currentDimension=2&Id=1&report=1&param=1

instead of:

https://site.com/bacon_report?Id=1&report=1&currentDimension=2&param=1


回答3:

The problem here is escaping - you need to escape the "&" characters. In XML all special characters like <, >, ', " and & should be escaped.

Escape it properly as

https://example.com/bacon_report?Id=1&amp;report=1&amp;currentDimension=2&amp;param=1

..just like in HTML:

<a href="https://example.com/bacon_report?Id=1&report=1&currentDimension=2&param=1">WRONG - no escaping</a>
<a href="https://example.com/bacon_report?Id=1&amp;report=1&amp;currentDimension=2&amp;param=1">CORRECT - correct escape sequence</a>

So - the cleanest way to show "&curren" in HTML/XML is to properly escape the ampersand, and render it as "&amp;curren".



回答4:

I think that in this case it is best to use htmlentities because with urlencode you get https%3A%2F%2Fexample.com%2Fbacon_report%3FId%3D1%26report%3D1%26currentDimension%3D2%26param%3D1

and when applying urldecode, you will still have the &curren symbol where as with htmlentities the url comes out clean.
https://example.com/bacon_report?Id=1&report=1&currentDimension=2&param=1



标签: php html url