Jump to content

fgets stringlength


maluch

Recommended Posts

I'm using a text file as a small database. in one of the pages i read in the text file one line at a time using fgets and compare it to a predetermined string. the problem that i am having is that the string brought in by fgets is always two characters longer than it should be. For example, say the file line says "hello" and i fgets that line and use strlen to find out the length it shows up as being 7 chars!!!! does fgets bring in a string with some extra chars? the text file im using is encoded in ANSI. thanks for the help!

Link to comment
https://forums.phpfreaks.com/topic/100852-fgets-stringlength/
Share on other sites

It probably has a carriage return "\r" and newline "\n" at the end of the string. Use trim() before using strlen():

 

http://us.php.net/manual/en/function.trim.php

 

Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.

 

Link to comment
https://forums.phpfreaks.com/topic/100852-fgets-stringlength/#findComment-515758
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.