2

Prevent conversion of HTML entities.

Please look at the code below:

$array = array(
  "id" => 123456,
  "currency" => "EUR",
);

var_dump(http_build_query($array));

//OUTPUT:
string 'id=123456&currency=EUR' (length=22)

Ok that works. But some servers give me this output:

string(25) "pspid=123456¤cy=EUR"

So, some servers treat &curren as an HTML entity. But I don't want that.

How can I avoid the unwanted conversion of HTML Entities?

Julian
  • 4,396
  • 5
  • 39
  • 51

2 Answers2

0

The solution is simple. Just use htmlentities on the output.

ini_set("display_errors", 1);
$array = array(
  "id" => 123456,
  "currency" => "EUR",
);
print_r(http_build_query($array));
echo PHP_EOL;
print_r(htmlentities(http_build_query($array)));

//OUTPUT:
id=123456&currency=EUR
id=123456&currency=EUR

Link: https://eval.in/803318

Julian
  • 4,396
  • 5
  • 39
  • 51
0

Check your php version since:

PHP 5.3.1 (Buggy Behavior) http_build_query DOES escape the '&' ampersand character that joins the parameters. Example: user_id=1&setting_id=2.

PHP 5.4+ http_build_query DOES NOT escape the '&' ampersand character that joins the parameters. Example: user_id=1&setting_id=2

For more details check here: https://stackoverflow.com/a/42317655/1016425

lloiacono
  • 4,714
  • 2
  • 30
  • 46