patternphpMinor
Check for correct number of elements in exploded string
Viewed 0 times
numberexplodedelementscorrectforcheckstring
Problem
Let's say I've got this string:
and I explode this like so:
what would be the best way to check if I've got the correct number of elements and whether they are numeric (because the input can vary)?
Straightforward, I would say something like this:
But you could also do:
You can use some array functions to check for the
$str = '1-2-3';and I explode this like so:
$strSplit = explode('-', $str);what would be the best way to check if I've got the correct number of elements and whether they are numeric (because the input can vary)?
Straightforward, I would say something like this:
if (isset($strSplit[0]) && is_numeric($strSplit[0]) && isset($strSplit[1]) && is_numeric($strSplit[1]) && isset($strSplit[2]) && is_numeric($strSplit[2]) {
// some code
}But you could also do:
if (count($strSplit) === 3 && is_numeric($strSplit[0]) && is_numeric($strSplit[1]) && is_numeric($strSplit[2])) {
// some code
}You can use some array functions to check for the
is_numeric part, but that seems fussy, less readable and is probably slower. I could be wrong, though...Solution
There are several options.
My favorite (based on readablity and simplicity) is using array_filter to remove everything that is not a number from the array and then comparing with the original to see if anything changed, then using count to make sure you have the correct number of elements:
Edit: did some speed testing and tried a few more options and validations.
I speedtested with the following inputs:
The results (after 1 million cycles) were as follows :
Static isset and is_numeric :
Looped isset and is_numeric :
count and array_filter :
My favorite (based on readablity and simplicity) is using array_filter to remove everything that is not a number from the array and then comparing with the original to see if anything changed, then using count to make sure you have the correct number of elements:
$str = '1-foo-3';
$strSplit = explode('-', $str);
if(count($strSplit) == 3 && $strSplit == array_filter($strSplit, 'is_numeric')){
// array is numeric // run code
} else {
// array is non-numeric
}Edit: did some speed testing and tried a few more options and validations.
I speedtested with the following inputs:
A: $strSplit = explode('-', '1-2-3');//3 elements, all numeric //evaluates true
B: $strSplit = explode('-', '1-foo-3-4');//4 elements, not all numeric //evaluates falseThe results (after 1 million cycles) were as follows :
Static isset and is_numeric :
if (isset($strSplit[0]) && is_numeric($strSplit[0]) && isset($strSplit[1]) && is_numeric($strSplit[1]) && isset($strSplit[2]) && is_numeric($strSplit[2]) && !isset($strSplit[3])) {
// some code
// A : speed 0.68 mircoseconds
// B : speed 0.48 mircoseconds
}Looped isset and is_numeric :
for($i=0; $i<3;$i++) if(!isset($strSplit[$i]) || !is_numeric($strSplit[$i])) $nonNumeric = true;
if(!isset($strSplit[($i+1)]) && !isset($nonNumeric)){
// some code
// A : speed 0.96 mircoseconds
// B : speed 1.00 mircoseconds
}count and array_filter :
if(count($strSplit) == 3 && $strSplit == array_filter($strSplit, 'is_numeric')){
// some code
// A : speed 2.00 mircoseconds
// B : speed 0.16 mircoseconds
}Code Snippets
$str = '1-foo-3';
$strSplit = explode('-', $str);
if(count($strSplit) == 3 && $strSplit == array_filter($strSplit, 'is_numeric')){
// array is numeric // run code
} else {
// array is non-numeric
}A: $strSplit = explode('-', '1-2-3');//3 elements, all numeric //evaluates true
B: $strSplit = explode('-', '1-foo-3-4');//4 elements, not all numeric //evaluates falseif (isset($strSplit[0]) && is_numeric($strSplit[0]) && isset($strSplit[1]) && is_numeric($strSplit[1]) && isset($strSplit[2]) && is_numeric($strSplit[2]) && !isset($strSplit[3])) {
// some code
// A : speed 0.68 mircoseconds
// B : speed 0.48 mircoseconds
}for($i=0; $i<3;$i++) if(!isset($strSplit[$i]) || !is_numeric($strSplit[$i])) $nonNumeric = true;
if(!isset($strSplit[($i+1)]) && !isset($nonNumeric)){
// some code
// A : speed 0.96 mircoseconds
// B : speed 1.00 mircoseconds
}if(count($strSplit) == 3 && $strSplit == array_filter($strSplit, 'is_numeric')){
// some code
// A : speed 2.00 mircoseconds
// B : speed 0.16 mircoseconds
}Context
StackExchange Code Review Q#51215, answer score: 6
Revisions (0)
No revisions yet.