6

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
  • You are creating a URL; there are rules you have to follow (url-encoding). Commented Oct 29, 2013 at 22:43

6 Answers 6

10

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
Sign up to request clarification or add additional context in comments.

Comments

9

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 

1 Comment

The link doesnt work anymore if I urlencode the comple url. Its also mentioned here webmasters.stackexchange.com/questions/5509/… that one should not do this.
6

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

1 Comment

You haven't demonstrated how to programmatically achieve this solution.
0

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

Comments

0

I came across this issue while working on technical documentation (in Markdown which gets converted to HTML).

To solve the issue I used a zero-width space character which I copied and pasted from between these brackets (​). That way it appears that there is no space and can include the below without any issues: /search?query=1&currentLonLat=-74.600291,40.360869

1 Comment

Adding a zero-width character may look ok, but may screw up the clickable link - then you may in effect change the parameter "currentLonLat" to "%e2%80%8bcurrentLonLat", where "e2%80%8b" is the correctly url encoded zero-width space character. A better solution would be to correctly encode the special characters, just like in your link /search?query=1&currentLonLat=-74.600291,40.360869 ...where if you look at the HTML source code it will be encoded as /search?query=1&amp;currentLonLat=-74.600291,40.360869, with the ampersand "&" correctly escaped to "&amp;".
0

This canoncial page needs someone to say "use http_build_query()" like several of the signpost dupes demonstrate.

Don't manually create query strings. Have a native PHP solution properly, reliably encode it for you.

If you need to parse your input strings as URL before encoding only the querystring of the URL, do that.

$params = [
    'Id' => 1,
    'report' => 1,
    'currentDimension' => 2,
    'param' => 1,
];

echo 'https://example.com/bacon_report?' . http_build_query($params);

If you are actually building HTML links, then:

$url = 'https://example.com/bacon_report';
$queryString = http_build_query([
    'Id' => 1,
    'report' => 1,
    'currentDimension' => 2,
    'param' => 1,
]);
printf(
    '<a href="%s?%s">%s</a>',
    $url
    $queryString,
    htmlentities("$url?$queryString")
);

More reading:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.