Jump to content

Search a word with query SELECT


christa

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.