Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Display Special Characters

Tags:

php

When i output the text £3.99 per M² from an xml file,browser displays it as £3.99 per M².XML file is in UTF-8 format.I wonder how to fix this.

like image 994
blakcaps Avatar asked Dec 27 '22 19:12

blakcaps


2 Answers

Make sure you're outputting UTF-8. That conversion sounds like your source is UTF-8, yet you're telling the browser to expect something else (Latin1?). You should send a header indicating to the browser UTF-8 is coming up, and you should have the correct meta header:

 <?php
 header ('Content-type: text/html; charset=utf-8');
 ?>
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 </head>
 <body>
 <?php echo "£3.99 per M²"; ?>
 </body>
 </html>

This should work correctly.

like image 93
Berry Langerak Avatar answered Jan 09 '23 08:01

Berry Langerak


You should encode html entities:

you could try

htmlentities($str, ENT_QUOTES, "UTF-8");

Look here for a complete reference

If you still have problems sometimes you also have to decode the string with utf8_decode() so you can try:

$str = utf8_decode($str);
$str = htmlentities($str, ENT_QUOTES);
like image 27
Nicola Peluchetti Avatar answered Jan 09 '23 10:01

Nicola Peluchetti