reading unicode characters into a string

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
himphp
Forum Newbie
Posts: 2
Joined: Thu Aug 07, 2008 2:16 am

reading unicode characters into a string

Post by himphp »

hi friends,

I am having a csv (comma separated value) file , each character in the file is in unicode format. i.e [a 2 byte character per character].
csv data:
======
and,10
in,20
win,30
======

See the following code:

$file=fopen("testdata.csv","r");
$ch = fread($file,"2"); initial 2 bytes to be read for unicode format.

while(!feof($file))
{
$ch = fread($file,"2");
echo $ch; // displaying character from the file.

$str1="a";
if(strcasecmp($ch,$str1) == 0)
{

}else{

}
}
Here even if character 'a' is in the file the comparison always shows false.
It happens for any character in the file.

I am not getting what is the issue.

Is the format read from the file(unicode) and the string used in the php program
different or is there any encoding required to either of the format.
I am completely unaware of the encoding & how to detect .

I am a total new new bie to php
plz help me urgent.

Thanks,
himanshu k.
User avatar
lukewilkins
Forum Commoner
Posts: 55
Joined: Tue Aug 12, 2008 2:42 pm

Re: reading unicode characters into a string

Post by lukewilkins »

Hi himphp,

I think you're better off using something like this.

Code: Select all

 
$file=fopen("testdata.csv","r");
$ch = fread($file,filesize('testdata.csv'));
$part = explode("\n",$ch);
$strComp = 'and';
foreach($part as $value){
    $part2 = explode(",",$value);
    if($part2[0]==$strComp){
        echo 'Yes! $strComp is equal to ' . $part2[0];
    }
}
 
The way you were reading the file you were actually never even comparing the 'a'. Try that out and let me know if you were trying to do something different. I don't think your issue has anything to do with unicode characters at all.

Hope that helps!

Luke

----------------
Now playing: M. Ward - Involuntary
via FoxyTunes
Post Reply