tags:

views:

62

answers:

2

I have some form values that include HTML entities, for example:

<option value="Coup&#232;"> Coup&#232; </option>

However, once the form is posted to the server, if I do a print_r($_POST); and then view the source of the page, the entity isn't there, its the actual accented character.

I suppose I can just run the post data though htmlentities but I'm wondering is this standard behavior for PHP? Or is this something I can turn off?

+9  A: 

PHP isn't; the browser is. You can check this with Firebug or a sniffer.

Ignacio Vazquez-Abrams
He said he viewed the source though...
alex
@alex The source of the page isn't decoded, the values of the form submission are.
Chacha102
The browser changes the value before it sends it as a post value.
Marius
@alex Right, entities in the source will be decoded by the browser while it reads the source and will henceforth be used in their decoded form. That's why `Coupè` displays as `Coupè` when viewed in the browser. Source !== DOM !== POST.
deceze
Ah, I understand now.
alex
I'm not sure I understand - do you mean the HTML entities in the VALUE are decoded and then submitted as the characters instead of the encoding?
Stomped
They are decoded to Unicode codepoints, then submitted with the character set encoding given in the page or form.
Ignacio Vazquez-Abrams
Simply put @Stomped, @Ignacio is saying 'Yes'.
Chacha102
A: 

The browser is decoding the entity when it encounters it, that's what they're for. The string &#232; means "Dear Browser, please replace this with the character 'è', because for whatever reason I could not write 'è' directly."

The browser will decode any entities used in the page, regardless of whether they're visible text or attribute values.

So, since the browser is decoding the entity, you'll have to encode the entity itself if you want to use a string that can be decoded as an entity:

Coup&amp;#232;

This will be decoded by the browser to

Coup&#232;

I'd question the use of this though, in this day and age and in most cases Unicode characters shouldn't pose a problem to warrant such encoding to begin with. Just write "Coupè" and be done with it. :)

deceze