3

Encoding an array to a URL using http_build_query() produces strange behaviour when an array key is also a html-char code.

For example:

return http_build_query([
   'id' = > ['my', 'data', 'here'], // no problem
   'class' = > ['my', 'data', 'here'], // no problem
   'yen' = > ['my', 'data', 'here'], // ¥ html car is ¥
   'parameter' = > ['my', 'data', 'here'], // ¶ html char is ¶
]);

and the encoded result is:

id[0]=my&id[1]=data&id[2]=here&class[0]=my&class[1]=data&class[2]=here¥[0]=my¥[1]=data¥[2]=here¶meter[0]=my¶meter[1]=data¶meter[2]=here

whats going on here, it cant be possible that i cannot use the word parameter as an array key.

4
  • 1
    its what you see in your browser, check out the view source Commented Jun 17, 2015 at 6:02
  • I don't understand how your code could produce such output. Please post actual code Commented Jun 17, 2015 at 6:03
  • yeah... Ghost is correct. I feel pretty silly... Commented Jun 17, 2015 at 6:12
  • when you want to debug in a browser, always make sure to htmlspecialchars() your output. Commented Jun 17, 2015 at 6:18

1 Answer 1

1

If you view the source of HTML output, you will see

id%5B0%5D=my&id%5B1%5D=data&id%5B2%5D=here&class%5B0%5D=my&class%5B1%5D=data&class%5B2%5D=here&yen%5B0%5D=my&yen%5B1%5D=data&yen%5B2%5D=here&parameter%5B0%5D=my&parameter%5B1%5D=data&parameter%5B2%5D=here

Which is correct. While displaying only, the browser will interpret malformed entities like &yen as ¥. There is nothing to worry about on the server side.

HTML entities reference

Demo: IDEOne

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

1 Comment

No, no. Don't. Sometimes this causes other issues. It was a valid question. For example, we wouldn't expect &parameter to be interpreted as ¶ which is ¶. Browser being overly helpful.

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.