Author Topic: Text with accents garbled after using includ  (Read 390 times)

0 Members and 1 Guest are viewing this topic.

Offline magarlickTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Text with accents garbled after using includ
« on: June 23, 2009, 02:59:57 PM »
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):
Code: [Select]
<?php 
include 'set_variable.php';
echo 
$foo;
?>

And this is what's in set_variable.php:
Code: [Select]
<?php
$foo 
'&#233;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 [code] tags
« Last Edit: June 23, 2009, 10:23:16 PM by zanus »

Offline celsoendo

  • Irregular
  • Posts: 41
  • Gender: Male
    • View Profile
Re: Text with accents garbled after using includ
« Reply #1 on: June 23, 2009, 09:45:21 PM »
Maybe your set_variable.php file is saved with incorrect encoding (different from the encoding of the main file).
Follow me on Twitter: http://twitter.com/celsoendo
<?php exit() or die(); ?>

Offline magarlickTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Re: Text with accents garbled after using includ
« Reply #2 on: June 24, 2009, 04:19:08 AM »
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.

Offline celsoendo

  • Irregular
  • Posts: 41
  • Gender: Male
    • View Profile
Re: Text with accents garbled after using includ
« Reply #3 on: June 24, 2009, 09:03:57 PM »
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
Code: [Select]
<?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:
Code: [Select]
<?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
« Last Edit: June 24, 2009, 09:05:54 PM by celsoendo »
Follow me on Twitter: http://twitter.com/celsoendo
<?php exit() or die(); ?>

Offline magarlickTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Re: Text with accents garbled after using includ
« Reply #4 on: June 25, 2009, 05:47:47 AM »
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.

Offline celsoendo

  • Irregular
  • Posts: 41
  • Gender: Male
    • View Profile
Re: Text with accents garbled after using includ
« Reply #5 on: June 26, 2009, 12:18:21 AM »
Hi!

I think these strange things is Dreamweaver's errors! Just an opinion, cause I really hate Dreamweaver.... lol!

You should use a PHP IDE for php programming and forget Dreamweaver... :)

Topic resolved!
Follow me on Twitter: http://twitter.com/celsoendo
<?php exit() or die(); ?>