1

I am writing a function php which has to do something like this:

  • we have two strings, ex: "Bienvenue" and "Bienbenue"

  • the two string don't match with the php function strpos.

  • but normally, the basic comportement of my function is to say, yes ok there is just one letter which differ between the two strings, so I return true. But I need some help for find an algorithm or an php function which exist?

  • In this example: "Bienvenue" and "Vienlpntw"

  • there is just "ien" which match the pattern, I have to see how many letters had matched and after that, comparate this number with the size of the word / pattern.

  • So we have 9 / 3 = 3; this ratio is bad, so I return false.

    but in this example: "interactivity" and "intevactizity", we have "inte" "acti" and "ity" which are matching. so we have 11 of 13 letters that are corrects. So it's not easy to manage errors of letters ...

For resume, I am looking for a php function which can tell me if an entry can match or not with my pattern, return true or false respecting these points.

2
  • Whats the application of this? It smells of homework... Commented Apr 20, 2012 at 17:56
  • Homework I don't think so, it's just for upgrade my research engine of my website Commented Apr 20, 2012 at 17:56

2 Answers 2

4

You can use the built-in function, similar_text to accomplish this. Make sure you check out the notes as well for some of the 'quirks'.

Based on your comments, you probably want to use something like this, which gets the longest common substring, and then divide it by string length and give it a threshold by which to return true or false

Sign up to request clarification or add additional context in comments.

4 Comments

I dont know how works this algorithm but it's not that I am looking for. This algorithm doesn't take in consideration the place of the letters in the string
It's not just see if for example the letter 'a' is contained in the string 'salut', the letter 'a' have to be the second letter in the first string, u see what i mean ?
@Necko That is a matter of opinion, I would say salut and ssalut are pretty similar, but according to your algorithm they are not.
@jeroen Yes they are similar also. So the function have to return true in this case because : salut is contained in 'salut'
1

here a very simple example

function precisionMatch($string1, $string2, $precision = 0.6666666) {

    $matchCount = 0;
    $string1Length = strlen($string1);
    $string2Length = strlen($string2);
    for($i = 0; $i < $string1Length; $i++) {

        if ($i < $string2Length && $string1[$i] == $string2[$i]) {

            $matchCount++;
        }
    }

    return $matchCount / ($string1Length == 0 ? 1 : $string1Length) > $precision;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.