There is no inbuilt PHP functions / methods that can do this as far as I know. How can I return the line number or the entire content of line in which a specific needle occurs? On failure, filegetcontents () will return false. You can then use key() to show the line number and current() (or getCurrentLine() or fgets()) to get the contents of that line, like so: // Create a new object for the fileĮcho 'Line #'. string filename, bool useincludepath false, resource context null, int offset 0, int length null ): stringfalse This function is similar to file (), except that filegetcontents () returns the file in a string, starting at the specified offset up to length bytes. You create a new object of this class with the filename of the file you're opening, then use seek() to seek to the line number of your choosing. This is best achieved using the SplFileObject class. When used with an object that implements the Countable interface, it returns the return value of the method Countable::count (). How can I move a file pointer to the start of a specific line number? count Counts all elements in an array or in a Countable object Description count ( Countable array value, int mode COUNTNORMAL ): int Counts all elements in an array when used with an array. This question does not meet Stack Overflow guidelines. Alternatively, you can use the standard file functions, fopen(), feof(), fgets(), and fclose(), to open the file and read it line by line, keeping a count as you go, like so: $count = 0 PHP way to get count of lines in file and to move file pointer to specified line closed Ask Question Asked 10 years, 11 months ago Modified 5 years, 9 months ago Viewed 16k times 3 Closed. However, this will use a lot of memory if your file is large, as it has to load the entire file into memory in order to count the return value from file(). Sample Solution: - PHP Code: To start out with, here is some basic variables: $filename = 'small_db.php'
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |