file() Feature Request - added support for file() to skip blank lines #80
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This was originally submitted as a bug report in https://bugs.php.net/41315
However, after discussion on IRC views about whether or not it was a defined behavior or a doc bug differed slightly. So I am submitting this as a feature request in part to include the additional desired behavior.
This will allow FILE_SKIP_EMPTY_LINES to skip lines that only contain the EOL character (blank lines that are non-empty strings to PHP). Previously the only way to achieve this was to use FILE_IGNORE_NEW_LINES with FILE_SKIP_EMPTY_LINES (thereby removing the EOL character(s) from the line causing to be an empty string to PHP).
This FR also added a new flag for the file() function, FILE_INDEX_LINES, which will return an array where the key indexes the line number in the file. It functions in accordance with FILE_SKIP_EMPTY_LINES as well where keys maintain the respective line number associated with their place in the file.
A good use case for this new addition (FILE_INDEX_LINES) would be where you want a reference of the line number in the original file (in the event you are modifying the contents of the array returned by file() and then rewriting the file where knowing line numbers is useful). You may want to keep track of which lines the array elements belong to in the file. For example, I've had some situations where I'm parsing logs and I keep reference points of which lines split the log rotation for different days identified by a specific line marker in the file. Current this is equally possible by offset as the array offset starts from 0 anyway, but if you chose to ignore empty lines that becomes a problem. Additionally, it may become confusing for the majority of novice users that aren't aware of the differences between an offsets (starting at 0) and counting up from integrals of 1. It requires the user pay extra attention to how they use the offset within their loops and from wherever else they access the array keys.
I think these new additions will be useful. Please, let me know if there are any suggestions, improvements, or additional features anyone feels might improve this PR and I'll try to make the necessary changes/additions as quickly as possible. Thanks.