fgetcsv() not working with special chars!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjsw
Forum Newbie
Posts: 10
Joined: Fri Jul 09, 2010 9:18 am

fgetcsv() not working with special chars!

Post by mjsw »

Hello,

I'm trying to perform simple operation in php, this is reading a csv file and displaying it on page. I've read the manual and everything, but php seems to be bugged as I can't get it to work properly with special characters (like accent chars in polish or any other language). CSV file has UTF8 encoding, page is also using UTF8. Here is the code:

Code: Select all

setlocale(LC_ALL, 'pl-PL.UTF-8');
if(($handle=fopen('file.csv',"r"))!==FALSE)
        {
            echo "<table>";
            while(($data=fgetcsv($handle,0,"\t","\""))!==FALSE)
            {
                $num=count($data);
                echo "<tr>\n";
                $row++;
                for($c=0;$c<$num;$c++)
                {
                    echo "<td>".$data[$c].' '.mb_detect_encoding($data[$c])."</td>\n";
                }
                echo "</tr>\n";
            }
            echo "</table>";
        }
        fclose($handle);
It displays:
1 ASCII Błąd w zapytaniu podczas usuwania obrazków: {0} UTF-8
instead of:
1 ASCII Błąd w zapytaniu podczas usuwania obrazków: {0} UTF-8
I have also tried using iconv and utf8_encode on the displayed data, with no luck. What is wrong here?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: fgetcsv() not working with special chars!

Post by AbraCadaver »

You're positive that your outputted page has this:

[text]<meta http-equiv="content-type" content="text/html; charset=UTF-8" />[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mjsw
Forum Newbie
Posts: 10
Joined: Fri Jul 09, 2010 9:18 am

Re: fgetcsv() not working with special chars!

Post by mjsw »

AbraCadaver wrote:You're positive that your outputted page has this:

[text]<meta http-equiv="content-type" content="text/html; charset=UTF-8" />[/text]
yes, it's there. there is no problem with page or file encoding in my case, everything is UTF-8. Problem is with this function...
Post Reply