Skip to content

file() Feature Request - added support for file() to skip blank lines #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

file() Feature Request - added support for file() to skip blank lines #80

wants to merge 1 commit into from

Conversation

srgoogleguy
Copy link
Contributor

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.

… without requiring FILE_IGNORE_NEW_LINES and added FILE_INDEX_LINES option for file() to return an array where the key indexes the line number in the file.
@dsp
Copy link
Member

dsp commented Jun 6, 2012

I think the idea of the patch is good. Let's wait if there are objections, otherwise I would like to see that in 5.5.

@@ -731,6 +733,12 @@ static void file_globals_dtor(php_file_globals *file_globals_p TSRMLS_DC)
use_include_path = flags & PHP_FILE_USE_INCLUDE_PATH;
include_new_line = !(flags & PHP_FILE_IGNORE_NEW_LINES);
skip_blank_lines = flags & PHP_FILE_SKIP_EMPTY_LINES;

/* Added a new line-number to array-key indexing optional flag by Sherif */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think crediting this way in a comment makes much sense and is more confusing than helping. Code comments should explain things.

@srgoogleguy
Copy link
Contributor Author

You're right. This was submitted a while back and since then we've discussed separating this into two different pull requests, one for skipping empty lines and one for using the FILE_INDEX_LINES feature. Also it was discussed on IRC to ammend SPLFileObject's behavior to be inline with this feature.

I will have to rethink this and come back with a better approach considering the suggestions johannes has brought up here.

Thanks :)

@php-pulls
Copy link

Comment on behalf of lstrojny at php.net:

Closing for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants