Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and Writing UTF-8 from Query String PHP

Tags:

php

utf-8

I need to be able to pass music sharp and flat symbols around in a query string and also display in a utf-8 browser. The displaying part is straight-forward by using html entity representations for sharp and flat symbols.

Flat -  ♭
Sharp - ♯

It's the passing around of this data that has me stumped.

Let's take the chord symbol B flat: B♭
It's representation with HTML entities is: B♭

Client
To pass it I url-encode it: B%26%239837%3B
Then I feed that in my query string: ?lookup=B%26%239837%3B

Server
Read it: $r = urldecode($_GET['lookup']);
Display it: echo $r; Just get B

So then I tried:

Read it: $r = $_GET['lookup'];
Display it: echo $r; Just get B

Also the raw request just shows B.

How do I pass this type of url-encoded html entity around correctly in the query string?

like image 414
Slinky Avatar asked Jul 19 '26 10:07

Slinky


1 Answers

This would be more straight-forward if you used actual UTF-8 encoded characters instead of HTML entities.

B%26%239837%3B is the URL encoded version of B♭. B♭ is the HTML entity representation of "B♭", it is not "UTF-8". What you want is B%E2%99%AD, which is the URL-encoded UTF-8 representation of "B♭". If you'd do $_GET['lookup'] on that, you'd get the UTF-8 encoded string "B♭".

You should keep everything in UTF-8 front to back. Please see Handling Unicode Front To Back In A Web App and possibly What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text for details.

like image 157
deceze Avatar answered Jul 21 '26 23:07

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!