christa Posted December 27, 2010 Share Posted December 27, 2010 hi all, in mysql, how can i extract 100 words before and 100 words after my search key? example: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla est nibh, mattis eu mattis id, pulvinar eu augue. Duis ut sem nisi. Sed id ante sed orci vestibulum lacinia ac id nibh. Donec cursus, elit eget auctor semper, ipsum eros laoreet quam, nec ullamcorper" vestibulum is my search key Link to comment https://forums.phpfreaks.com/topic/222752-search-a-word-with-query-select/ Share on other sites More sharing options...
litebearer Posted December 28, 2010 Share Posted December 28, 2010 Presuming your search returned a row having a field named words that contained... "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla est nibh, mattis eu mattis id, pulvinar eu augue. Duis ut sem nisi. Sed id ante sed orci vestibulum lacinia ac id nibh. Donec cursus, elit eget auctor semper, ipsum eros laoreet quam, nec ullamcorper" you could (psuedo code) while($row = mysql_fetch_array($result)) { $string = $row['words']; $my_array = explode("vestibulum", $string); $my_array[0] would contain all the words before the search word $my_array[1[ would contain all the words after the search word $front_array = explode(" ", $my_array[0]); $back_array = explode(" ", $my_array[1]; simply count the number of elements in the front and back arrays and remove those outside your range criteria Link to comment https://forums.phpfreaks.com/topic/222752-search-a-word-with-query-select/#findComment-1151999 Share on other sites More sharing options...
christa Posted December 28, 2010 Author Share Posted December 28, 2010 i try this code: while ($row = mysql_fetch_array($result)) { $my_array = explode("$mykey", $row['mytext']); $my_array[0]; $my_array[1]; $front_array = explode(" ", $my_array[0]); $back_array = explode(" ", $my_array[1]); echo $front_array[0] . " <b>$mykey</b> " . $back_array[1]; } but it returns some errors: "Notice: Undefined offset: 1" and without words nor left nor right. Link to comment https://forums.phpfreaks.com/topic/222752-search-a-word-with-query-select/#findComment-1152266 Share on other sites More sharing options...
litebearer Posted December 28, 2010 Share Posted December 28, 2010 try this... while ($row = mysql_fetch_array($result)) { $my_array = explode($mykey, $row['mytext']); $front_array = explode(" ", $my_array[0]); $back_array = explode(" ", $my_array[1]); echo $my_array[0] . " <b>" . $mykey . "</b> " . $my_array[1]; } Link to comment https://forums.phpfreaks.com/topic/222752-search-a-word-with-query-select/#findComment-1152275 Share on other sites More sharing options...
christa Posted December 28, 2010 Author Share Posted December 28, 2010 ok, but how can i extract or print only 50 words before and 50 words after $mykey? Link to comment https://forums.phpfreaks.com/topic/222752-search-a-word-with-query-select/#findComment-1152296 Share on other sites More sharing options...
litebearer Posted December 28, 2010 Share Posted December 28, 2010 this is JUST an example of how it is done, look it over throughly. ( here is live example in action http://nstoia.com/tutorials/search.php ) <?PHP /* this is your search word */ $search_word = "vestibulum"; /* this is the text you are searching */ $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla est nibh, mattis eu mattis id, pulvinar eu augue. Duis ut sem nisi. Sed id ante sed orci vestibulum lacinia ac id nibh. Donec cursus, elit eget auctor semper, ipsum eros laoreet quam, nec ullamcorper"; /* presuming there is ONLY one occurrence of your search word in the text, this will creat and array with all the wrods preceding your search word in the first array element and all the words following your search word in the second element */ $string_array = explode($search_word, $string); /* this takes all the words that precede the search word and puts them into an array */ $front_array = explode(" ", $string_array[0]); /* this takes all the words that follow the search word and puts them into an array */ $back_array = explode(" ", $string_array[1]); /* now we count the words in the $front_array */ $num_front_words = count($front_array); /* and we count the words in the $back_array */ $num_back_words = count($back_array); /* this is the number of words we want */ $num_words = 50; /* we now to check and see IF there are more than 50 words preceding the search word */ if($num_front_words>$num_words) { $start = $num_front_words - $num_words; $front_phrase = implode(" ", array_slice($front_array, $start, $num_words)); }else{ $front_phrase = implode(" ", $front_array); } /* we now to check and see IF there are more than 50 words following the search word */ if($num_back_words>$num_words) { $start = $num_back_words - $num_words; $front_phrase = implode(" ", array_slice($back_array, 0, $num_words)); }else{ $back_phrase = implode(" ", $back_array); } echo $string . "<p>the above string has " . $num_front_words . " words before " . $search_word . " and " . $num_back_words . " words after " . $search_word . "<hr>"; $string ="Lorem ipsum at feugiat ornatus luptatum eam. Pri cu iudico primis habemus, ea mandamus neglegentur sit. Ad eos soleat maiestatis abhorreant, vim no sale putent. Hinc convenire comprehensam ne has, in duo doming sapientem referrentur.Cu eum vivendum prodesset suscipiantur, vix nulla lucilius id. Ad per vero indoctum evertitur, ea consul postea aliquando vim. Ei adhuc gloriatur eam. Velit apeirian te vim, ad iracundia conceptam nam. Ea illum quidam assueverit nec, an mea prima maiorum. Duis ut sem nisi. Sed id ante sed orci vestibulum lacinia ac id nibh. Vivendo expetendis et sed, his id voluptua pertinax periculis. Ullum posidonium reprimique eam ex, ex quo ignota possit. Ne qui petentium splendide. Soluta scaevola nec cu. Timeam scripta ne ius. Modus expetenda ius ne, in eos iusto nostrud. Et vix exerci euismod concludaturque, ei vix mutat recteque deseruisse, vide inimicus vim te. Indoctum tincidunt ei quo, mei ei feugiat consequat, mei omnium delenit voluptua at. Detracto necessitatibus ne pri, per quidam labitur facilis no. Dicant appareat theophrastus eum an, cum eu atqui intellegebat."; $string_array = explode($search_word, $string); $front_array = explode(" ", $string_array[0]); $back_array = explode(" ", $string_array[1]); $num_front_words = count($front_array); $num_back_words = count($back_array); if($num_front_words>$num_words) { $start = $num_front_words - $num_words; $front_phrase = implode(" ", array_slice($front_array, $start, $num_words)); }else{ $front_phrase = implode(" ", $front_array); } if($num_back_words>$num_words) { $start = $num_back_words - $num_words; $back_phrase = implode(" ", array_slice($back_array, 0, $num_words)); }else{ $back_phrase = implode(" ", $back_array); } echo $string . "<p>the above string has " . $num_front_words . " words before " . $search_word . " and " . $num_back_words . " words after " . $search_word . "<p>"; echo "The " . $num_words . " that precede " . $search_word . " are: <p>" . $front_phrase . "<p> and the " . $num_words . " that follow " . $search_word . " are:<p>" . $back_phrase; ?> Link to comment https://forums.phpfreaks.com/topic/222752-search-a-word-with-query-select/#findComment-1152349 Share on other sites More sharing options...
christa Posted December 29, 2010 Author Share Posted December 29, 2010 now it works fine. One question only: if the key search is (example) "House" the code works; if the key search is "house" the code returns: Notice: Undefined offset: 1 in.... line of $back_array = explode(" ", $string_array[1]); Why? there is a case insensitive example? Link to comment https://forums.phpfreaks.com/topic/222752-search-a-word-with-query-select/#findComment-1152541 Share on other sites More sharing options...
litebearer Posted December 29, 2010 Share Posted December 29, 2010 Here is some info re that issue ... here is a tested case-insensitive explode I named it explodei().works cool :-) <?php function explodei($separator, $string, $limit = false ) { $len = strlen($separator); for ( $i = 0; ; $i++ ) { if ( ($pos = stripos( $string, $separator )) === false || ($limit !== false && $i > $limit - 2 ) ) { $result[$i] = $string; break; } $result[$i] = substr( $string, 0, $pos ); $string = substr( $string, $pos + $len ); } return $result; } ?> If your php version is under 5, you'll need to add stripos() to your script. See http://php.net/function.stripos source: http://theserverpages.com/php/manual/en/function.explode.php Link to comment https://forums.phpfreaks.com/topic/222752-search-a-word-with-query-select/#findComment-1152546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.