maluch Posted April 12, 2008 Share Posted April 12, 2008 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 More sharing options...
toplay Posted April 12, 2008 Share Posted April 12, 2008 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 More sharing options...
maluch Posted April 13, 2008 Author Share Posted April 13, 2008 its works! thanks a lot. Link to comment https://forums.phpfreaks.com/topic/100852-fgets-stringlength/#findComment-515808 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.