Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include html page charset problem

after querying a mysql db using the code below i have generated an html file:

    $myFile = "page.htm";

$fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $row['text']); fclose($fh);

On the msql db the text is encoded using utf8_general_ci. But i need to include it in a php web page as shown below:

 <?include('page.htm');?>

bearing in mind that the php web page uses utf8 charset on the header:

<meta http-equiv="content-type" content="text/html; charset=utf8" />

Now if i write on the db some letters with grave accent (è à ì) or a quote character and i open directly page.htm and on the db i see it all looking ok, but when i view it on the php page i see a question mark � character instead of those I originally wanted. Why?! Thanks in advance!

like image 485
EnneKappa Avatar asked Nov 29 '22 05:11

EnneKappa


2 Answers

The problem is that the encoding of the html page is actually set by the http response header 'Content-Type', to fix what you need to do is add the following to your PHP file before any output (ie at the top).

<?php
header('Content-Type: text/html; charset=utf8');

To clarify, that should be in the PHP that includes your html file, not in the html file you include :)

side point(s):

  1. It's good practise to use the full opening tag <?php rather than <? as this isn't supported by all (many) servers
  2. include is a statement not a function, so typically you'd write: include 'page.htm';
like image 123
nathan Avatar answered Dec 04 '22 23:12

nathan


META charset isn't always solve the problem. Make sure your IDE saving real UTF-8 files. For exanple in Dreamweaver press CTRL-J then check Title/Encoding options.

like image 44
fabrik Avatar answered Dec 04 '22 23:12

fabrik