-
Notifications
You must be signed in to change notification settings - Fork 493
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
Don't parse all numbers using if ( is_numeric( ... ) )
#147
Comments
Yes, I know this. What I'm saying is: It is not great how this currently works. Converting every potential number is not a good idea. Please let us developers control what gets converted to a number and what doesn't. |
Oh, yes, I'm sorry. The color Anyway, I've looked a little more into the default:
$value = (string) $cell->v;
/* I'm not sure how to really check for the format here. But something like this. */
if ( $cell['format'] !== '@' && is_numeric( $value ) ) {
/* ... do number conversion ... */
} Would that be ok? EDIT: I've looked into the source code. I see it's not that simple. |
Currently, every value gets checked if it's a number using
is_numeric()
. There are some types of values that pass this check depending on the concrete value. ZIP codes for instance:12345
=>number
, but01234
=>string
. Or color codes:000000
=>number
, butffffff
=>string
.This makes working with the data really difficult. If JS consumes this data using an API and I want to convert all color codes to lowercase it would look like this:
If I knew every color was a string I could just do:
I have a few ideas how to solve this:
1) Add an option whether numbers should be parsed or not
This syntax requires PHP 8.0 or later. Users of older versions would need to write:
This could be made nicer using an options array because you can skip all options you don't want to change:
I know, the options array makes the library more complex and is only helpful for older PHP versions. So, this is totally optional IMO.
If the option is set to
false
numbers can still be parsed later usingintval()
orfloatval()
. So, it's still pretty flexible.2) Store the original value in a separate field
Easier to implement I guess, but probably does unnecessary work and needs a bit more memory.
The text was updated successfully, but these errors were encountered: