0

I have an array that contains some strings, am trying to find particular string is present in an array, I tried to use array_search() it is working but it is searching for exact string, is there any way to find similar string and returns its position. Below is my example.

$prodInfoStr = 'banana_2_3_150';
$myArr = Array ( [0] => apple_3_1_180 [1] => apricot_4_100_65 [2] => banana_2_3_135 );
$searchRes = trim(array_search($prodInfoStr,$myArr ));

$searchRes returns 2. it is perfectly fine.

If I search for banana_2_4_200, result should return me 2 so that I can replace it.

6
  • 2
    What exactly is your definition of "similar strings"? Commented Jun 4, 2014 at 9:38
  • I dont see banana_2_3_150 in your $myArr Commented Jun 4, 2014 at 9:41
  • If is search for banana_2_3_150 that should return 2, similar in the sense starting letter should match. Commented Jun 4, 2014 at 9:50
  • What should it return if you search for apple_4_100_65? Should it return apricot_4_100_65 or apple_3_1_180? Commented Jun 4, 2014 at 9:59
  • should return apple_3_1_180 Commented Jun 4, 2014 at 10:19

2 Answers 2

1

You could try this,

// input word
$input = 'banana_2_3_150';

// array of words to check against
$words  = array('apple_3_1_180','apricot_4_100_65','banana_2_2.5_135');

// no shortest distance found, yet
$shortest = -1;

// loop through words to find the closest
foreach ($words as $word) {

    // calculate the distance between the input word,
    // and the current word
    $lev = levenshtein($input, $word);

    // check for an exact match
    if ($lev == 0) {

        // closest word is this one (exact match)
        $closest = $word;
        $shortest = 0;

        // break out of the loop; we've found an exact match
        break;
    }

    // if this distance is less than the next found shortest
    // distance, OR if a next shortest word has not yet been found
    if ($lev <= $shortest || $shortest < 0) {
        // set the closest match, and shortest distance
        $closest  = $word;
        $shortest = $lev;
    }
}


 echo "Matched Key =>" . array_search($closest,$words);

I have taken this code from php.net and modified. Refer the below link for more

http://www.php.net/manual/en/function.levenshtein.php

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

Comments

0

You can calculate a Levenshtein distance for each string and then pick the string with the smallest distance:

$myArr = Array ( 'apple_3_1_180', 'apricot_4_100_65', 'banana_2_2.5_135' );

$prodInfoStr = 'banana_2_3_150';

$scores = array();
foreach($myArr as $item)
    $scores[$item] = levenshtein($item, $prodInfoStr);

asort($scores);
echo "the most similar string is ", key($scores);

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.