views:

217

answers:

4

Hi everyone,

Quick question, how can I make this valid :

if($this->datos->bathrooms == "1½"){$select1 = JText::_( 'selected="selected"' );}

The ½ doesn't seem to be recognized. I tried to write it as ½ but then it looks for ½ literally, and not the ½ sign. Any ideas?

A: 

Try comparing it with "1½"

chromano
+1  A: 

The character you are looking for is the Unicode character Vulgar Fraction One Half

There are a multitude of ways to make sure you are displaying this character properly, all of which depend on the encoding of your data. By looking here we can see that

  • In ISO-8859-1, a popular western encoding, this character is encoded as BD
  • In UTF-8, a popular international encoding, this character is encoded ad C2BD

What this means is that if your PHP file is UTF-8 encoded, but you are sending this to the browser as ISO-8850-1 (or the other way around), the character will not render properly.

As others have posted, you can also use the HTML Entity for this character which will be character-encoding agnostic and will always render (in HTML) properly, regardless of the output encoding.

Peter Bailey
Downvoted? Really?
Peter Bailey
@Peter: Upvoted to compensate :-)
Josh
+4  A: 

As many others have noted, you have a character encoding problem, most likely. I'm not sure what encodings PHP supports but you need to take the whole picture into account. For this example I'm assuming your PHP script is responding to a FORM post.

  1. Some app (yours, most likely) writes some HTML which is encoded using some encoding and sent to the browser. Common choices are ISO-8859-1 and UTF-8. You should always use UTF-8 if you can. Note: it's not the default for the web (sadly).
  2. The browser downloads this html and renders the page. Browsers use Unicode internally, mostly, or some superset. The user submits a form. The data in that form is encoded, usually with the same encoding that the page was sent in. So if you send UTF-8 it gets sent back to you as UTF-8.
  3. PHP reads the bytes of the incoming request and sets up its internal variables. This is where you might have a problem, if it is not picking the right encoding.
  4. You are doing a string comparison, which decomposes to a byte comparison, but the bytes that make up the characters depends on the encoding used. As Peter Bailey wrote,

    • In ISO-8859-1 this character is encoded as 0xBD
    • In UTF-8 this character is encoded as 0xC2BD

You need to verify the text encoding along each step of the way to make sure it is happening as you expect. You can verify the data sent to the browser by changing the encoding from the browser's auto-detected encoding to something else to see how the page changes.

If your data is not coming from the browser, but rather from the DB, you need to check the encodings between your app and the DB.

Finally, I'd suggest that it's impractical to use a string like 1½ as a key for comparison as you are. I'd recommend using 1.5 and detecting that at display time, then changing how the data is displayed only. Advantages: you can order the results by number of bathrooms if the value is numeric as opposed to a string, etc. Plus you avoid bugs like this one.

Mr. Shiny and New
Excellent explanation. Joomla 1.5+ stores data in UTF-8, however, PHP is not natively UTF-8: you have to use the multibyte functions. Joomla has its own set of multibyte-safe string functions that can be used as an alternative.And yes, doing 1.5 instead of trying to depend on keying by 1½ would be best in this situation.
jlleblanc
Thank you for the great detail in there, I did follow your suggestion in the end and simply made the display change the value.
skarama
A: 

Use the PHP chr function to create the character by its hex 0xBD or dec 189:

if($this->datos->bathrooms == "1".chr(189)){$select1 = JText::_( 'selected="selected"' );}
Ricket