10

The following PHP code:

<?php
$s = "page.com?a=1&current";
echo $s;

outputs the unexpected:

page.com?a=1¤t

Setting the file encoding to utf-8 didn't work. When I set $s to "page.com?a=1&curre", the output is as expected.

I want to know if this is because in my project I have $url, which needs to be appended with "&currentPage=1".

What is causing this problem?

4
  • the output is ok. the browser view &curren; :-).
    – Eugen
    Commented Dec 26, 2016 at 20:12
  • Sorry but I can't reproduce the problem, perhaps you should add other elements to your question. Commented Dec 26, 2016 at 20:12
  • @Eugen that's true, but if this happens when echoing a link to the page, things can get messed up.
    – sidyll
    Commented Dec 26, 2016 at 20:17
  • @sidyll ah so, ok.
    – Eugen
    Commented Dec 26, 2016 at 20:19

5 Answers 5

14

That's the entity code for the currency symbol being interpreted. If you're building your GET url, you can solve it in various ways:

  • Use urlencode() on your query values:

    $s = 'page.com?' . urlencode("a=1&currentPage=2");

  • Use the entity for & itself;

    'page.com?a=1&amp;currentPage=2'

  • Or use your variable at the beginning so no & is required:

    'page.com?currentPage=2&a=1'

0
3

Ampersands & need to be converted into HTML special characters (using e.g. htmlspecialchars or urlencode, or simply typing it in). Your output into HTML should look like this:

$s = "page.com?a=1&amp;current";

Otherwise they may collide with HTML entities, as happens in your example. The HTML entity for ¤ is &curren;, and for reasons unknown to me, these entities match in HTML even without the closing ;. Edit: As to why and when this happens, keyword being historical reasons, read here.

2
  • They are not valid, but are matched by most parsers in browsers anyway. Maybe for backwards compatibility
    – sidyll
    Commented Dec 26, 2016 at 20:20
  • Yup. Just added in a link with more background on why this happens.
    – Markus AO
    Commented Dec 26, 2016 at 20:22
0

problem is because of &curren is short code for ¤ sign. so whenever you put &current it took it as ¤t.

to solve this you can use urlencode function

$s = urlencode("page.com?a=1&current");
echo $s;

Hope this will solve your problem.

2
  • &curren; is an html entity but not &curren Commented Dec 26, 2016 at 20:13
  • but somehow it transform it without ;
    – reza
    Commented Dec 26, 2016 at 20:14
0

If you are displaying the string on screen then you just need to replace all & characters with &amp; JUST FOR DISPLAYING ON SCREEN. Don't actually replace the characters in the url string like this else the url will not work correctly.

eg.

printf( 'this is my string: %s' , str_replace('&', '&amp;', $yourFullString) );

0

Simple solution is convert the & to the encode format like &amp;. This will work for me. Here is example

URL."reference=1&amp;currency=";

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.