Question

I am simulating an address comparison that will determine the difference of a user's input to a google map api output. Let's say that $str1=is the user input and $str2=is the google output.

I use the split to break the strings into arrays and strcasecmp to compare them. My question is how to compare all the array values that are split, currently it only compares 199a and 199 in this part. $var = strcasecmp($strarr[0], $strarr2[0]);

Update...........

<?php
$str1 = "199a Freestone Road, Sladevale QLD 4370, Australia";
$str2 = "199 Freestone Road, Sladevale QLD 4370, Australia";

$strarr = (explode(" ",$str1));
echo("<br>");

$strarr2 = (explode(" ",$str2));
echo("<br>");

$var = strcasecmp($strarr[0], $strarr2[0]);
echo"$var";

if($var===0)
{
    echo("The Strings are equal.");
}

elseif ($var==!0) 
{
    echo("<br>");
    echo("wafasd");  
}

I appreciate all your suggestions on just directly compare the two arrays,yes it may solve the current comparison problem, but due to some parts of the current project I am working on I am not sure if it is suited. I'll elaborate it below

User's Input: 199a Sladevale Road, Sladevale QLD 4370, Australia
Google Map API Output: 199 Freestone Road, Sladevale QLD 4370, Australia

Changes(this is what I am trying to achieve):
199a is changed by Google into 199
Sladevale is changed by Google into Freestone

Était-ce utile?

La solution

If both arrays are at the same length then you can just loop through them:

for($i=0;$i<count($strarr);$i++) {
    $var = strcasecmp($strarr[$i], $strarr2[$i]);
    echo $var."<br>";
}

And use explode() instead of split(). split() is deprecated and should not be used, because it could be removed in the next version of PHP. explode() does exactly the same.

Autres conseils

You can just do if ($strarr==$strarr2) to compare to arrays. Another way is to use array_diff in both directions.

$str1 = "199 Freestone Road, Sladevale QLD 4370, Australia";
    $str2 = "199a Freestone Road, Sladevale QLD 4370, Australia";

    $strarr = (explode(" ",$str1));
    echo("<br>");

    $strarr2 = (explode(" ",$str2));
    echo("<br>");

    if ($strarr == $strarr2) {echo "equal array";} else {echo "not equal array";}

This works..

<?php
$str1 = "199a Freestone Road, Sladevale QLD 4370, Australia";
$str2 = "199 Freestone Road, Sladevale QLD 4370, Australia";

$strarr1 = split(" ",$str1);
echo("<br>");

$strarr2 = split(" ",$str2);
echo("<br>");
$count = count($strarr1);
for($i=0;$i<$count;$i++){
$flag = 0;
$var = strcasecmp($strarr1[$i], $strarr2[$i]);
if($var===0)
{
    $flag =0;//match
}

elseif ($var==!0) 
{
    $flag = 1;//mismatch
    echo "strings not match!";
    exit;
}
}
if($flag ==0)
echo "strings match!";

?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top