Page 1 of 1

reading unicode characters into a string

Posted: Thu Aug 07, 2008 2:39 am
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.

Re: reading unicode characters into a string

Posted: Tue Aug 12, 2008 8:22 pm
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