Jump to content

Help needed with arrays in PHP


gazfocus

Recommended Posts

I am trying to add a text box with a spell checker to my website, and I have got an array called $dictionary with the words in, and then I have a checkbox (which is $_POST[text]).

 

The code I have so far is:

 

if ($_POST[spell] == 'yes')

{

$check = explode(" ",$_POST[text]);

foreach ($dictionary as $currentword)

{

foreach ($check as $usercurrentword)

{

$notfound = true;

if ($usercurrentword == $currentword)

{

$notfound = false;

$output = $output . $output;

}

else

{

$output = $output . "<font color=\"blue\"><ul>" . $usercurrentword . "</ul></font>";

}

 

}

}

echo $output;

}

 

But this just produces several copies of the output (corresponding with the amonut of words in the dictionary).

 

Can anyone advise where I am going wrong?

 

Thanks

Link to comment
Share on other sites

Simplify

 


<?php

if ($_POST[spell] == 'yes') {
$words = explode(" ",$_POST[text]);
foreach ($words as $word) {
	if ( empty($word) )
		// Skip empty values
		continue;

	if ( !in_array($word, $dictionary) )
		$errors[] = $word;

}

print_r($errors);

}

?>

 

As an example

Link to comment
Share on other sites

Otherwise this should fix what you have.

if ($_POST[spell] == 'yes')
     {
     $check = explode(" ",$_POST[text]);
     foreach ($check as $usercurrentword)
        {
        foreach ($dictionary as $usercurrentword)
           {
           $notfound = true;
           if ($usercurrentword == $currentword)
              {
              $notfound = false;
              $output = $output . $output;
              }
           else
              {
              $output = $output . "<font color=\"blue\"><ul>" . $usercurrentword . "</ul></font>";
              }

           }
        }
    echo $output;
    }

 

I took the echoed output out of the loop so you won't get it a ton of times and reversed the foreach() loops you had so that the main listing is for the text itself and the 2nd one is checking each word in the dictionary against it.

Link to comment
Share on other sites

Otherwise this should fix what you have.

if ($_POST[spell] == 'yes')
     {
     $check = explode(" ",$_POST[text]);
     foreach ($check as $usercurrentword)
        {
        foreach ($dictionary as $usercurrentword)
           {
           $notfound = true;
           if ($usercurrentword == $currentword)
              {
              $notfound = false;
              $output = $output . $output;
              }
           else
              {
              $output = $output . "<font color=\"blue\"><ul>" . $usercurrentword . "</ul></font>";
              }

           }
        }
    echo $output;
    }

 

I took the echoed output out of the loop so you won't get it a ton of times and reversed the foreach() loops you had so that the main listing is for the text itself and the 2nd one is checking each word in the dictionary against it.

 

I just tried this code and it just echos each word in the dictionary array twice.

 

Any ideas? Thanks

Link to comment
Share on other sites

Sorry I forgot to change the key for the dictionary. Additionally I cleaned it up a little bit, as it was, if the words matched you wouldn't have displayed the word at all.

Try this here:

if ($_POST[spell] == 'yes')
      {
      $check = explode(" ",$_POST[text]);
      foreach ($check as $usercurrentword)
         {
         foreach ($dictionary as $currentword)
            {
            $notfound = true;
            if ($usercurrentword == $currentword)
               {
               $notfound = false;
               $output .= " " . $usercurrentword;
               }
            else
               {
               $output .= " <font color=\"blue\"><ul>" . $usercurrentword . "</ul></font>";
               }

            }
         }
     echo $output;
     }

Link to comment
Share on other sites

Sorry I forgot to change the key for the dictionary. Additionally I cleaned it up a little bit, as it was, if the words matched you wouldn't have displayed the word at all.

Try this here:

if ($_POST[spell] == 'yes')
      {
      $check = explode(" ",$_POST[text]);
      foreach ($check as $usercurrentword)
         {
         foreach ($dictionary as $currentword)
            {
            $notfound = true;
            if ($usercurrentword == $currentword)
               {
               $notfound = false;
               $output .= " " . $usercurrentword;
               }
            else
               {
               $output .= " <font color=\"blue\"><ul>" . $usercurrentword . "</ul></font>";
               }

            }
         }
     echo $output;
     }

Thanks for your help :) I really appreciate it.

 

When I try that code, I get

 

the the the the the the the the the the the the the the the the james james james james james james james james james james james james james james james james james

 

the word 'the' is in the dictionary, and 'james' isn't.

Link to comment
Share on other sites

if ($_POST[spell] == 'yes')
{
$check = explode(" ", $_POST[text]);
      foreach ($check as $usercurrentword)
         {
         $notfound = true;
         foreach ($dictionary as $currentword)
            {
            if ($usercurrentword == $currentword)
               $notfound = false;
            }
         if ($notfound == false) {
            $output .= " " . $usercurrentword;
         } else {
            $output .= " <font color=\"blue\"><b>" . $usercurrentword . "</b></font>";
         }
         }
     echo $output;
}

Tested this time! And it works! I replaced the underline script with bold because it was screwing up the format on my browser O.o anyway, I set up a custom message and a short dictionary and it turned out ok for me. Hopefully this will be the last update :)

Link to comment
Share on other sites

if ($_POST[spell] == 'yes')
{
$check = explode(" ", $_POST[text]);
      foreach ($check as $usercurrentword)
         {
         $notfound = true;
         foreach ($dictionary as $currentword)
            {
            if ($usercurrentword == $currentword)
               $notfound = false;
            }
         if ($notfound == false) {
            $output .= " " . $usercurrentword;
         } else {
            $output .= " <font color=\"blue\"><b>" . $usercurrentword . "</b></font>";
         }
         }
     echo $output;
}

Tested this time! And it works! I replaced the underline script with bold because it was screwing up the format on my browser O.o anyway, I set up a custom message and a short dictionary and it turned out ok for me. Hopefully this will be the last update :)

 

Whooooooooo it worked...many thanks. You're a star!

Link to comment
Share on other sites

As it stands, you'll run into issues with capitalization. You can overcome this by changing this line:

 

if ($usercurrentword == $currentword)

 

To:

 

if (strtolower($usercurrentword) == $currentword)

 

And making sure every word in your dictionary is in lower case.

 

Though you will still have issues with punctuation.

Link to comment
Share on other sites

As it stands, you'll run into issues with capitalization. You can overcome this by changing this line:

 

if ($usercurrentword == $currentword)

 

To:

 

if (strtolower($usercurrentword) == $currentword)

 

And making sure every word in your dictionary is in lower case.

 

Though you will still have issues with punctuation.

Thanks. That's helpful. Is there a way around the punctuation issue? (maybe is there a way to mass delete any punctuation during the explode?)

 

Thanks again

Link to comment
Share on other sites

You'd have to go into regular expressions. Im sure it would be much more efficient to do this with the split, otherwise you'd have the regex overhead on every single comparison. I don't think that could be easily incorporated into the above. So try:

 

<?php

$dictionary = array('this','is','a','test','dictionary');
$sentence = 'This? is a "test" sentence'; 

$words = preg_split('/(\.|\?|!| |,|;|:|&|\'|")/',$sentence,-1,PREG_SPLIT_NO_EMPTY);//if you need to add other punctuation, separate it with a pipe(|). If its a special character, you'll need to stick a backslash before it.
foreach($words as $k => $v){
    $words[$k] = strtolower($v);
}
$errors = array_diff($words,$dictionary);
foreach($errors as $v){
    $sentence = str_ireplace( $v ,'<font color="blue"><b>'.$v.'</b></font>',$sentence);
}
echo $sentence;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.