How to get ¤ to display literally, not as an

2019-01-09 18:02发布

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?

标签: php html url
4条回答
对你真心纯属浪费
2楼-- · 2019-01-09 18:09

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".

查看更多
The star\"
3楼-- · 2019-01-09 18:16

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

查看更多
来,给爷笑一个
4楼-- · 2019-01-09 18:18

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
查看更多
Bombasti
5楼-- · 2019-01-09 18:27

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 
查看更多
登录 后发表回答