paulmo Posted December 30, 2009 Share Posted December 30, 2009 I've got several "if/elseif" arrays that follow the code below, where message ($word) comes from submitted text form that contains sentences with ANY words. problem is, only the LAST word in message is being searched against my if/elseif arrays. I need EVERY word from message searched. so my $word variable isn't right. my //comments are here to show different things I've played with. $message = $_POST['message']; $post_message = ucfirst($message); $message = mysql_real_escape_string($message); $message = strip_tags($message); //$message = trim ($message); $pre_filter=trim($message); $get_search=explode(" ",$pre_filter); //$post_message = stripslashes($post_message); //$where = ''; foreach ($get_search as $word) { $word = trim($word); // $skip = false; use this later } Quote Link to comment https://forums.phpfreaks.com/topic/186651-text-form-exploded-into-individual-words-help/ Share on other sites More sharing options...
joel24 Posted December 30, 2009 Share Posted December 30, 2009 the trim function is removing any spaces... then you're trying to explode it by any spaces, were you after ltrim() and rtrim()? that would remove the whitespace etc from the start and the end, then you can explode it after that. Quote Link to comment https://forums.phpfreaks.com/topic/186651-text-form-exploded-into-individual-words-help/#findComment-985789 Share on other sites More sharing options...
paulmo Posted December 30, 2009 Author Share Posted December 30, 2009 I've tried with and without pre_filter(trim). Still, only last word of string is being searched, whereas I need EVERY word searched. Thanks again in advance. $message = strip_tags($message); $message = mysql_real_escape_string($message); //$pre_filter=trim($message); $wordchunks = explode(" ", $message); for($i = 0; $i < count($wordchunks); $i++){ echo "Word: $i = $wordchunks[$i] <br />"; } //$message = preg_split("/[\s,]+/")($message); foreach ($wordchunks as $word) { $word = trim($word); $skip = false; } Quote Link to comment https://forums.phpfreaks.com/topic/186651-text-form-exploded-into-individual-words-help/#findComment-985981 Share on other sites More sharing options...
premiso Posted December 30, 2009 Share Posted December 30, 2009 $wordchunks = explode(" ", $message); $where = "(searchedfield LIKE '%" . implode("%' OR searchedfield LIKE '%", $wordchunks) . "%')"; Should generate a where clause that searches for each word in that array. Quote Link to comment https://forums.phpfreaks.com/topic/186651-text-form-exploded-into-individual-words-help/#findComment-986012 Share on other sites More sharing options...
paulmo Posted December 30, 2009 Author Share Posted December 30, 2009 thanks for the suggestion; please help fine tune. still not working. i'm sure i'm supposed to do something with new $where variable--just don't know what. $wordchunks = explode(" ", $message); $where = "(message LIKE '%" . implode("%' OR message LIKE '%", $wordchunks) . "%')"; for($i = 0; $i < count($wordchunks); $i++){ echo "Word: $i = $wordchunks[$i] <br />"; } //$message = preg_split("/[\s,]+/")($message); foreach ($wordchunks as $word) { $word = trim($word); $skip = false; } if arrays follow... Quote Link to comment https://forums.phpfreaks.com/topic/186651-text-form-exploded-into-individual-words-help/#findComment-986031 Share on other sites More sharing options...
paulmo Posted December 31, 2009 Author Share Posted December 31, 2009 some guidance on $where variable please? Quote Link to comment https://forums.phpfreaks.com/topic/186651-text-form-exploded-into-individual-words-help/#findComment-986412 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.