Best way to compare two strings
I need to map various mobile features (about 30 features). But the comparison should be based on values ββsuch as "800 mAh lithium-ion battery".
Suppose mobile "A" has a battery size of "800mAh Li-ion battery"
and the second mobile "B" has a "Li-ion Battery Battery", 400 mAh "
then A is better than B.
So how can I compare 800mAh and 400mAh, ignoring the dormant Li-ion battery,
in almost all 30 functions I need to do for such a comparison. How am I supposed to make a comparison at 4Mb vs 1GB.
I am currently trying to use the list function ($ a, $ b) = explode (). Is there a better solution.
It will depend on the lines. Given the two you provided as a limited sample, you can ignore non-numeric characters (also going out in periods) and then compare numbers. Something like that:
$number_a = preg_replace( '/[^0-9.]/', '', $string_a;
$number_b = preg_replace( '/[^0-9.]/', '', $string_b;
Then compare them as numbers:
if ( $number_a > $number_b ) {
...
}
This works as long as you always compare the same units, for example, you show examples. But if the units are different from each other, for example "800mAh, Li-ion battery" versus "Li-ion Battery, .4 AH", then this will not happen. You will need to add some more logic to analyze and interpret the units.
How about extracting the number from your string and comparing them
$A = filter_var('800 mAH, Li-ion Battery', FILTER_SANITIZE_NUMBER_INT);
$B = filter_var('Li-ion Battery, 400 mAH', FILTER_SANITIZE_NUMBER_INT);
if($A > $B){
// A is better
}