Jump to content

Match string with array


Xafke

Recommended Posts

Hi guys.

 

I wanna write a script that checks how similar a string is to any string in an array.

 

Let's say this is my array:

 

"Name1" => "ABCD"

"Name2" => "ABCDE"

 

And let's say my string is ABC. The script should check how similar ABC is to any string in the array.

So my string matched Name1 for 85%. And it matches name2 for 75%.

 

The script should output the name (in this case name1 or name2) and should output howmuch it matched.

 

I've tried with similar_text() but it can't deal with arrays.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/195101-match-string-with-array/
Share on other sites

can you descibe how this 85% or 75% is calculated?

 

"ABC" has 3 characters, "ABCD" has 4 characters, so i would say they match for 3/4, of 75%

"ABC" has 3 characters, "ABCDE" has 5 characters, so i would say they match for 3/5, of 60%

 

Anw what about if you try to match "ABC" to "XYZABCDE" ?

<?php

$text[1]="ABCD";
$text[2]="ABCDE";

$compare="ABC";

$m=0;
foreach ($text as $x) {
        $p="";
        similar_text($compare, $x, &$p);
        $p=round($p,3);
        if ($p>$m) {
                $result="$x compares to $compare: $p <br>";
                $m=$p;
        }
        echo "$x compares to $compare: $p <br>";
}

echo "Max is:";
echo $result;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.