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?

5
  • This is just output. If you pass this url to server - everything will be okay Commented May 23, 2017 at 9:03
  • What php version are you using? There are a few differences on how http_build_query works in different php versions Commented May 23, 2017 at 9:05
  • @u_mulder Yes, but why is &curren converted to Entities and on other servers it is not converted? Commented May 23, 2017 at 9:06
  • @lloiacono php 5.3.10 ... not the most recent version lol Commented May 23, 2017 at 9:06
  • 1
    @Julian Have a look on this eval.in/803318 Commented May 23, 2017 at 9:08

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

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.