8

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?

MJH
  • 2,301
  • 7
  • 18
  • 20
user1712263
  • 115
  • 1
  • 7

5 Answers5

12

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'

sidyll
  • 57,726
  • 14
  • 108
  • 151
2

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.

Community
  • 1
  • 1
Markus AO
  • 4,771
  • 2
  • 18
  • 29
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.

reza
  • 1,507
  • 2
  • 12
  • 17
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) );

Mark B
  • 528
  • 5
  • 7
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=";
Nikunj K.
  • 8,779
  • 4
  • 43
  • 53