steve.davis Posted August 15, 2009 Share Posted August 15, 2009 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/ Share on other sites More sharing options...
ignace Posted August 15, 2009 Share Posted August 15, 2009 What does $ex contain? Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-898987 Share on other sites More sharing options...
steve.davis Posted August 15, 2009 Author Share Posted August 15, 2009 $ex = explode(' ', $essay); essay is text that is typed into a textarea form. You can see the demo at: http://www.davislegal.net/essays/sample_form.php Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-899004 Share on other sites More sharing options...
ignace Posted August 15, 2009 Share Posted August 15, 2009 $ex = explode(' ', $essay); meaning $ex contains North, Dakota (two separate words) and not North Dakota (one whole word) Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-899011 Share on other sites More sharing options...
steve.davis Posted August 15, 2009 Author Share Posted August 15, 2009 meaning that if I type "One of my friends lives in Alabama and the other lives in North Dakota" the [0] alabama is found but not [1] north dakota Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-899012 Share on other sites More sharing options...
steve.davis Posted August 15, 2009 Author Share Posted August 15, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-899046 Share on other sites More sharing options...
Jessica Posted August 15, 2009 Share Posted August 15, 2009 Rather than using explode, try strpos() for each state. If the strpos is not false, you know the state is in there. There may be a way to do it with an array but this is the first way that comes to my mind. Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-899049 Share on other sites More sharing options...
steve.davis Posted August 15, 2009 Author Share Posted August 15, 2009 Thanks for the tip. I tried it but receive an error. I'll spend some time understanding the command, but it looks like it finds the position of the first char in the string. Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-899081 Share on other sites More sharing options...
Jessica Posted August 15, 2009 Share Posted August 15, 2009 Correct. So you know as long as it returns a number 0 or more, it was found. So if it finds it, you can add that state to your list of ones found. Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-899101 Share on other sites More sharing options...
steve.davis Posted August 16, 2009 Author Share Posted August 16, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-899296 Share on other sites More sharing options...
Prismatic Posted August 16, 2009 Share Posted August 16, 2009 <?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 ) Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-899329 Share on other sites More sharing options...
thebadbad Posted August 19, 2009 Share Posted August 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/170421-in_array-multiple-word-problem/#findComment-901476 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.