EnneKappa
EnneKappa

Reputation: 239

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!

Upvotes: 4

Views: 30315

Answers (8)

will
will

Reputation: 69

I know i'm 10 years late for OP, but I encountered same problem. I was doing an include of a php file (I supose it's the same for html file) that wouldn't show utf-8. I couldn't use meta as this include comes after displaying html.

My way to solve:

  1. Copied whole text of php into a new file.
  2. Replaced original php with new file.

I hope it helps, i supose some meta data in original file was breakig everyting. But this is jsut a guess.

Upvotes: 1

nimura
nimura

Reputation: 11

Try separate includes blocks. Ex mainpage.php:

<?php
 include("php1.php");
?>

<?php
 include("php2.php");
?>enter code here

Upvotes: 1

Hamed MP
Hamed MP

Reputation: 5503

One point about your code:

when you use include, it is a php code, so you should include a php file, not a htm file:

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

try changing the extension, I don't know how your code were working till now :)

Upvotes: 1

ChrisW
ChrisW

Reputation: 21

If you are getting stuff from a database, and it changes charset inexplicitly, note that in Php the actual connection charset to the db needs to be set explicitly to utf8 (if that's what you use), otherwise the content is converted while transfered, even if the content in the db itself is in correct format.. an interesting quirk ;)

Like so: mysql_select_db($database, $connect); mysql_set_charset('utf8', $connect); // set the connection charset.

Upvotes: 2

EnneKappa
EnneKappa

Reputation: 239

I solved adding this header('Content-Type: text/html; charset=iso-8859-1'); in the php webpage with the include. I don't know why it works. I never used iso-8859-1 charset. Thanks anyway!

Upvotes: 5

fabrik
fabrik

Reputation: 14365

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.

Upvotes: 7

nathan
nathan

Reputation: 5452

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';

Upvotes: 6

Unicron
Unicron

Reputation: 7456

The problem is probably with a wrong encoding set in your mySQL database, or the database connection.

If your tables are all 100% utf8_general_ci, try doing a mysql_query("SET NAMES utf8;"); before doing any queries: That will set the connection to UTF-8.

Upvotes: 1

Related Questions