Struggling with fgets()

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
El Vasco
Forum Newbie
Posts: 15
Joined: Tue Jan 04, 2011 12:05 pm

Struggling with fgets()

Post by El Vasco »

Hi All,
I am trying to get a Random word from a .txt file I wrote with Notepad.
To do that:
1.I Open the File.
2.I Get a random position for $fp:
$size=filesize($dictionary);
$rand_location=rand(0,$size);
fseek($fp,$rand_location);
3.I use $word=fgets($fp, 7) to get a 6 caracter long word...
4.Strangely I get an only 3 visible caracter word plus some kind of null caracter hidden...
i.e.: lets assume that I pick the string 'abc' from the file, in order to trouble-shoot I do the following:
echo $word; // and I get 'abc'
$word=addslashes($word);
echo $word; // and I get '\0a\0b\0c'
strlen($word); // I get '6'

This have me quite confused and I have no clue with this happens. Any hint is more than Welcome :-) !

Br
El Vasco
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Struggling with fgets()

Post by requinix »

You probably saved the file in UTF-16 format.
El Vasco
Forum Newbie
Posts: 15
Joined: Tue Jan 04, 2011 12:05 pm

Re: Struggling with fgets()

Post by El Vasco »

Thanks a Lot, tasairis ! You are completely right ! Problem solved !
Thanks Again !
El Vasco
Post Reply