2

I don't want to echo string if before string is similar to current string. Let's say our strings are,

$strings = array("software","software","game","antivirus");

My difference function,

function ($val1,$val2) {
similar_text($val1,$val2,$percent);
if ($percent>83) {
// should not echo. But don't know how to do.
}
}

But I don't know how can I do it. I guess it should be with using for each.

3
  • Where does $percent and similar_text() come from? But if you don't want to echo above 83% then simply change it to $percent<=83 Commented Feb 4, 2012 at 13:20
  • @lethalMango $percent is automatically generated by php's similar_text() function. Before rating topics, please make sure revise your scholar about the situation. Commented Feb 4, 2012 at 13:30
  • So I asked a question to understand your problem for my reference more. If I had known the answer I'd have answered myself. Before assuming I had rated topics, ensure you know who rates them. Commented Feb 4, 2012 at 19:25

3 Answers 3

2

Try something like this:

$strings = array("software","software","game","antivirus");

$lastString = '';

foreach ($strings as $string) {
    similar_text($lastString, $string, $percent);
    if ($percent < 83) {
        echo "$string<br />";
        $lastString = $string;
    }
}

If you don't understand some part of it, leave a comment and I will clarify.

Edit:
I moved the $lastString = $string; inside the condition.

Consider the following list of strings:
$strings = array("software","sofware","sofwart","ofwart","fwart","wart","warts");

Leaving the $lastString assignment outside of the loop would only print software even though lots of the words are very very different software they are not so different from the previous word.

Moving it inside actually gives the output :

software
sofwart
wart

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

1 Comment

Yeah, actually these strings were just an example. I will use this on sentences.
1
$strings = array("software","software","game","antivirus");
$previous = '';
foreach ($strings as $string) {
  if ($string===$previous) {
    continue;
  } else {
    echo $string;
    $previous = $string;
  }
}

But I think it's better to do it with for like this (it should be faster):

$strings = array("software","software","game","antivirus");
$num = count($strings);
for ($i=0;$i<$num;$i++) {
  if ($strings[$i]===$strings[$i-1] && $i!==0) {
    continue;
  } else {
    echo $strings[$i];
  }
}

Btw I totally did't get what the $percent means..

2 Comments

Why if(cond) { continue; } when you can do if (!cond). Also you only cater for exact matches which is not what is asked for.
@madxpol $percent is the percentage of similarity which is generated by php's similar_text() function.
1

An approach using array_filter() (assumes >= 5.3):

$strings = array('software', 'software', 'game', 'antivirus');

$filtered = array_filter($strings, function($curr) {

    static $prev; 

    similar_text($prev, $curr, $percent);
    $prev = $curr;   

    if ($percent < 83) {
        return $curr;
    }        
});

print_r($filtered);

Yields:

Array
(
    [0] => software
    [2] => game
    [3] => antivirus
)

Hope this helps. Actually, I never knew about similar_text() until now. Pretty interesting function. Thanks :)

2 Comments

Thanks Darragh. Leigh's script worked fine for me. But I will try yours also. If this works, I will test and use the fastest one.
ha. yes, I just saw Leigh's was marked as answered when I posted! actually, some of the array_* functions are slow enough so I'd bet his will be faster.

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.