Jump to content

Text with accents garbled after using includ


magarlick

Recommended Posts

Hi all.

 

I have an odd problem with accented letters becoming garbled. Consider this snippet (I have the charset set to UTF-8 in the meta content of this document):

<?php

$foo = 'étoile';

echo $foo;

?>

The above works fine. The text is printed with the accent intact. But now look at this. First, a modification to the above (again, same charset):

<?php 
include 'set_variable.php';
echo $foo;
?>

And this is what's in set_variable.php:

<?php
$foo = 'étoile';
?>

If I do it like this, then the accented e in the variable $foo is garbled. A lozenge with a question mark. I have no idea why. I cannot pass any strings with accented characters in them. They all come out garbled.

 

I looked in the php.ini file and set default_charset = "utf-8". Same. So I cut it out completely. Same. So I set default_charset = "iso-8859-1". Still garbled but not a lozenge this time.

 

I looked in the httpd.conf and added this:

 

#AddCharset UTF-8 .utf8

#AddDefaultCharset UTF-8

 

Same. The problem just will not go away.

 

Does anybody have any idea how to fix this?

 

Thanks for any input.

 

Mark

 

 

I use Vista Home Premium, PHP 5.2.9-2, Apache 2.2.11.

 

note: use can use

 tags 

Thank you, celsoendo.

 

How do I set the encoding then when saving a php file? I use Dreamweaver CS3. I have not actively set any encoding for my PHP files, except those that have an HTML header in them. So I do not see why some would have one encoding, and others would have another.

 

Cheers.

 

M.

Hi.

 

I don't know how to config dreamweaver cause I never used it, but here is the result of my tests:

 

set_variable.php

 

<?php

$foo = 'étoile';

$foo2 = '&#233;toile';

?>

 

main.php

<?php
include 'set_variable.php';

echo $foo . '<br />';
echo $foo2 . '<br />';
?>

 

Ok. The code above printed out $foo with wrong characters and $foo2 with accents.

 

Changed main.php to:

<?php
include 'set_variable.php';

header('Content-type: text/html; charset=utf-8');

echo $foo . '<br />';
echo $foo2 . '<br />';
?>

 

And it worked with $foo and $foo2. :)

 

Cheers

Thanks again for replying.

 

Yes I tried including that header statement before but it had no effect.

 

In fact I have solved the problem: the included file was corrupt, although it looked perfectly normal. I created a blank php file, copied the contents of the old include_variable.php into it, then saved as include_variable.php, overwriting the original file. It worked right away.

 

Who knows why these things are necessary!

 

I appreciate your help.

 

Mark.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.