0

I have a form where I can input two words then compare the levenshtein score, that works fine.

I want to be able to compare 1 word with a string of words delimited by ", ". The whole lot then needs to echo out. Here's what I have so far:

Levenstien for <b><?php echo $_POST["source"]; ?></b> and <b><?php echo $_POST["target"]; ?></b>:

<?php
$string5 = $_POST["source"];
$string6 = $_POST["target"];
$array6 = explode(', ',$string6); 

echo levenshtein("$string5","$array6");
?>
avasal
  • 14,350
  • 4
  • 31
  • 47
user1721230
  • 317
  • 1
  • 6
  • 19

1 Answers1

0

You'd use a foreach loop (a for loop would be fine too). Try:

Levenstien for <b><?php echo $_POST["source"]; ?></b> and <b><?php echo $_POST["target"]; ?></b>:

<?php
$string5 = $_POST["source"];
$string6 = $_POST["target"];
$array6 = explode(', ',$string6); 

foreach ($array6 as $derp)
{
    echo $string5, "/", $derp, ": ", levenshtein($string5, $derp), "<br>";
}

?>

To output in order by score, use this:

Levenstien for <b><?php echo $_POST["source"]; ?></b> and <b><?php echo $_POST["target"]; ?></b>:

<?php
$string5 = $_POST["source"];
$string6 = $_POST["target"];
$array6 = array_map('trim', explode(',',$string6));
$doop = array();

foreach ($array6 as $derp)
{
    doop[$derp] = levenshtein($string5, $derp);
}

arsort($doop); // sorts highest element first

foreach ($words as $key => $value)
{
    echo sprintf('%s / %s: %s<br />', $string5, $key, $value);
}

?>
Wug
  • 12,956
  • 4
  • 34
  • 54
  • It's been a long time since I've written anything in php. I'm pretty sure this is syntactically correct though. – Wug Oct 04 '12 at 20:21
  • That is absolutely amazing!!! Can I output that list from smaller to larger levenstien scores? – user1721230 Oct 04 '12 at 20:41
  • I don't think that worked I get a server error? Thank you for your help it is really, really appreciated – user1721230 Oct 04 '12 at 21:09
  • I patched in your edit. Using array_flip isn't required so I skipped it. Review the editing guidelines to ensure your edits are not rejected in the future. – Wug Oct 05 '12 at 13:31