0

I searched the system but couldn't find any help I could understand on this, so here goes...

I need to find an approximate match for a string in php.

Essentially I'm checking that all the $names are in the $cv string and if not it sets a flag to true.

foreach( $names as $name ) {
    if ( strrpos( $cv, $name ) === false ) {
        $nonameincv = true;
    }
}

It works fine. However, I had a case of $cv = "marie_claire" and a $name = "clare" which set the flag (of course) but which I'd have liked for strpos to have "found" as it were.

Is it possible to do an approximate match so that if a string has 1 extra letter anywhere in it, it would match? For example so that:

$name = "clare" is found in $cv = "marie_claire"

$name = "caire" is found in $cv = "marie_claire"

$name = "laire" is found in $cv = "marie_claire"

and so on...

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
arathra
  • 155
  • 1
  • 11
  • 1
    is this db-related by any chance? if so, there could be an alternative https://dev.mysql.com/doc/refman/5.7/en/string-functions.html – Funk Forty Niner Apr 17 '17 at 14:30
  • This is a deep, deep rabbit hole I'm afraid. How similar exactly is "similar enough"? Obviously "clare" and "marie_claire" are far from exact matches; how far do you want to go…? Is "ie_c" still valid? It's an exact sub-match. Or "clear"? Or "mrclr"? – deceze Apr 17 '17 at 14:34
  • 1
    For PHP [similar_text](http://php.net/manual/en/function.similar-text.php) or [levenshtein](http://php.net/manual/en/function.levenshtein.php). For DB, `FULL TEXT SEARCH`. – Gabriel Heming Apr 17 '17 at 14:34
  • you want to use the `levenshtein` function, example and docs http://php.net/manual/en/function.levenshtein.php – cmorrissey Apr 17 '17 at 14:34
  • For future reference, [php approximate string matching](https://www.google.com/#q=php+approximate+string+matching) leads to both of the functions Gabriel Heming references. GIYF. – alanlittle Apr 17 '17 at 14:56
  • @arathra The example you stated above contains difference of 1 character while searching. Do you just want this. Or the difference can be more than that. – Sahil Gulati Apr 17 '17 at 15:03
  • @Sahil-Gulati it would just be 1 character difference. – arathra Apr 18 '17 at 16:31
  • @arathra Okay after seeing your question i have created a script, I am sharing it with you. – Sahil Gulati Apr 18 '17 at 16:31
  • @arathra i have posted an answer on your question hope this will help you out. – Sahil Gulati Apr 18 '17 at 16:45

2 Answers2

0

Try this, not considering performance, but would work for your case.You can play with the the number of different chars deviation you want to accept.

$names = array("clare", "caire", "laire");
$cv = "marie_claire";

foreach( $names as $name ) {
    $sname = str_split($name);
    $words = explode('_', $cv);
    foreach($words as $word) {
        $sword = str_split($word);
        $result = array_diff($sword, $sname);
        if(count($result) < 2)
            echo $name. ":true\r\n";
    }
}
swaveg
  • 113
  • 5
  • Thanks; the problem here was having to find all the possible variables for the $names array; @sahil's code did that. – arathra Apr 20 '17 at 13:02
0

Note: This will work perfectly fine when there is difference of 1 character, as stated in question above.

Try this code snippet here

<?php
ini_set('display_errors', 1);
$stringToSearch="mare";
$wholeString = "marie_claire";

$wholeStringArray=  str_split($wholeString);
for($x=0;$x<strlen($wholeString);$x++)
{
    $tempArray=$wholeStringArray;
    unset($tempArray[$x]);
    if(strpos(implode("", $tempArray),  $stringToSearch)!==false)
    {
        echo "Found: $stringToSearch in ".implode("", $wholeStringArray);
        break;
    }
}
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42