domingo, 1 de febrero de 2009

The hell of nsbp or char 160 or %A0

I was coding a website and I came with an issue, it seems that if you use select option then add the text, well that normally works UNLESS you end up with double spaces, so if you have something like "HELLO HOW ARE YOU" and you place it in an option you will end with "HELLO HOW ARE YOU", if you are working with thins, products maybe that have several spaces and you can't change the product name, what do you do ?

well you can add &NBSP so it would end up begin something like "HELLO    &NBSPHOW ARE YOU" that will result in the correct text inside the option !.... now here comes the pain in the ARSE !

if you grab that data and try to include it on a database it will not enter as you would expect "HELLO HOW ARE YOU" but it will enter as:

"HELLO%A0%A0%A0HOW ARE YOU" ( %A0 is character 160 )

** note even here the damn Blog ate the SPACES .... I will re-write
but basically most of the text =


so when you try to do more "transactions" on the name, you will get an error, because you would normally write something like:

$sql="update value="newvalue" where salute="HELLO HOW ARE YOU" ( just a code stub )

but inside $SQL you already have saved the text with char(160)... so this:


is not the same as: (%20 is the normal space)

"HELLO%20%20%20%20%HOW ARE YOU"

so ... well this kept me up all the night it has to be corrected like this:

( in PHP )

str_replace("%A0"," ",DATA WITH NBSP);

that will replace the unholy NSBPs to real spaces...

then you can add it to the DB and work as you please.

this really gave me a headache today... geez.

No hay comentarios: