0

I need to check if an array of words ($words) is present in a larger array ($dictionary). If all the words are there, no errors. If one or more are not included in $dictionary, I want to send out an error message. So far, I have come up with this:

<?php
// first I select a column from a MySQL table and retrieve 
//all the words contained in that field.
$spell = "SELECT * FROM eventi WHERE utente='{$_SESSION['username']}'";
$qspell = mysql_query($spell) or die ("Error Query [".$spell."]");
while ($risu = mysql_fetch_array($qspell)){
    $risu = mysql_fetch_array($qspell);
    // the following lines remove parentheses, digits and multiple spaces
    $desc = strtolower($risu["descrizione"]);
    $words = explode(" ",$desc);
    $words = str_replace("(","",$words);
    $words = str_replace(")","",$words);
    $words = preg_replace('/[0-9]+/','',$words);
    $words = preg_replace('/\s+/',' ',$words);
    // the array $dictionary is generated taking a long list
    //of words from a txt file
    $dictionary = file('./docs/dizionario.txt',FILE_IGNORE_NEW_LINES);
    foreach($words as $k => $v){
        if (in_array($v, $dictionary)){
            //Do something?
        } else {
            $error = "error";
            echo "The word ".$v." can't be found in the dictionary.";
        }
    }
}
if (!isset($error)){
    echo "All the words are in the dictionary.";
} else {
    echo "There are some unknown words. See above.";
}
?>

This code always returns one single error message, without reporting which word can't be found. On top of that, words which are actually missing are not detected. What am I doing wrong?

Alex
  • 497
  • 5
  • 22
  • 3
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Oct 24 '16 at 18:42
  • I am aware of this problem. Thank you. – Alex Oct 24 '16 at 18:43
  • 1
    Can we get the error? – nerdlyist Oct 24 '16 at 18:46
  • It simply returns `The word can't be found in the dictionary.` (only once, and as you can see `$v` is empty). – Alex Oct 24 '16 at 18:47
  • 1
    What does your `$words` array look like ? Try doing `var_dump($words)` maybe ? – Maximus2012 Oct 24 '16 at 18:54
  • Also, try to make sure that `$dictionary` has valid values as well. – Maximus2012 Oct 24 '16 at 18:55
  • 1
    Why are you using `mysql_real_escape_string` o a value fetched from the database? – moni_dragu Oct 24 '16 at 18:57
  • Here is what `$words` look like: `array(6) { [0]=> string(8) "riunione" [1]=> string(2) "di" [2]=> string(14) "programmazione" [3]=> string(8) "triennio" [4]=> string(3) "con" [5]=> string(8) "colleghi" }` – Alex Oct 24 '16 at 19:02
  • You are right. I removed that. – Alex Oct 24 '16 at 19:03
  • Are you sure the file is being loaded properly? – Don't Panic Oct 24 '16 at 19:04
  • Yes: **http://php.net/manual/en/function.file.php** – Alex Oct 24 '16 at 19:10
  • Look at array_diff() – useyourillusiontoo Oct 24 '16 at 19:20
  • Using `array_diff()` it returns a series of empty arrays and then this: `Array ( [4] => ) Array ( [4] => ) Array ( [4] => ) Array ( [4] => ) Array ( [4] => ) Array ( [4] => )` – Alex Oct 24 '16 at 19:31

1 Answers1

0

Apparently, the problem lies at the line:

$words = preg_replace('/[0-9]+/','',$words);

Removing digits somehow messes the whole matching procedure.

Without removing digits, my code works.

Alex
  • 497
  • 5
  • 22