Jump to content

in_array multiple word problem


steve.davis

Recommended Posts

I can't seem to find a solution to an issue related to the in_array command.  If my array contain single words, it works fine.  But if the array contains words  :facewall:separated by a space, in_array does not work.

 

Example:

$states = array();

$states[0] = "Alabama";

$states[1] = "North Dakota";

 

in_array finds Alabama but not North Dakota.  Does anyone have suggestions for how to make in_array work with multiple words or can you suggest another command to do the same thing?

 

foreach($states as $id=>$word){

if(in_array($word,$ex)){

$correct++;

}

 

Link to comment
Share on other sites

I really appreciate any help you guys can provide.  I have been working on this for days and can't seem to get it right.  There MUST be an easy way to do this!  A visitor types words into a form which is submitted and those words are compared to words in an array using the in_array command.

Link to comment
Share on other sites

Use of the command strpos does work - THANKS!!  Now I'm have the problem with similar words.  Example: If I enter "AL ALA ALABAMA" all three words are found using strpos.  I understand why this happens, but I need it to be an exact match.  I've tried to use explode but that doesn't find any of the words.

 

<snip>

$ex = explode(' ', $essay);

// Loop through all of the $key_words array

foreach($key_words as $id=>$word){

$StringPos = strpos($essay,$word);

if ($StringPos === false) {

    echo "The keyword '$word' was not found in the string '$essay'<br>";

 

} else {

    echo "The keyword '$word' was found in the string '$essay'<br>";

    echo " and exists at position $StringPos<br>";

    $correct++;

    echo "Inside strpos:".$correct."<br>";

}

<snip>

 

That Works, but if I use $ex instead of $essay, it does not work.  Any thoughts?

 

Link to comment
Share on other sites

<?php
$states = array("California", "North Dakota", "Rhode Island", "Virginia");

$essay = "Theres a small restaurant just north of North Dakota where you can find
          clam chowder, similar to that found in Rhode Island. Virginia has terrible
          sea food and California imports all it's sea food from Mexico";
          
foreach($states as $state)
{
    if(preg_match('/'. $state .'/i', $essay))
    {
        $found[] = $state;
    }
}

print_r($found);
?>

 

Output

Array
(
    [0] => California
    [1] => North Dakota
    [2] => Rhode Island
    [3] => Virginia
)

Link to comment
Share on other sites

In regular expressions you can also use word boundaries. E.g. \bAl\b will only match Al in Al lives in Alabama. Code example:

 

<?php
$search = 'Al';
$essay = 'Al lives in Alabama, more than 1500 miles from North Dakota.';
$count = preg_match_all('~\b' . preg_quote($search, '~') . '\b~i', $essay, $matches);
echo "$search was found $count time(s) in the essay.";
?>

 

preg_quote() prepares $search for the regular expressions pattern, escaping special characters. the i after the closing pattern delimiter ~ makes the search case-insensitive.

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.