phpmysqlhtml

Converting currency symbols '$' '£' to html code $ £?


I stored the symbols in a table in a database, e.g.

country | currency_code | currency_symbol | currency_rate
UK        GBP             £                 1.0000
EU        EUR             €                 1.XXXX
USA       USD             $                 1.XXXX
Canada    CAD             $                 1.XXXX

The problem is that the currency symbols are not valid in html so they show up as instead.

Do I need to add another column to my table for HTML codes e.g. £ € $ or is there a simple function in PHP that I can use to wrap around the character to convert it to HTML?


Solution

  • Currency symbols are perfectly "HTML safe". What you have is an encoding problem. You are outputting these characters encoded in one encoding (likely Latin-1), but your browser is interpreting them in some other encoding (likely UTF-8). The actual encoding and the declared encoding need to match. Follow this answer or Handling Unicode Front To Back In A Web App to see where your problem is.