0

I'm searching a mysql database getting results which match the searched keyword. When the keyword is found within the search string I want to show the searcher that keyword before cutting the showed string down to 500 chars, therefore I move back from the keyword 1 char at a time until a "." is found, and make two positions after the fullstop the start of my string which is showed.

Maybe that wasn't explained well, hopefully you'll understand when you see the code...

while ($row = mysql_fetch_array($result)) { 
  $title = $row["title"];
  $content = nl2br($row["content"]); 

  $begin = strpos($content, $var); //find position of keyword
  
  while($begin != stripos($content, ".")) { //until fullstop is found
  $begin--;  //move positions back
  }
  $begin = $begin+2; //dont include full stop in displayed message
  $content = substr($content, $begin, 500); //get beginning of string + 500 chars to show

  echo "Title:$title<br>Content:$content";
}

The problem is that often, I don't think its always finding the fullstop, sometimes it appears to do so, otherwise it doesn't (depending on the keyword searched).

Please help me find a fix to this!

2
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by asif49
0

Your algorithm needs changing, 2 things I can think of that would break it:

Searching for "brown" in "The quick brown fox."

Searching for anything in a string without a period.

Edited by Smeagel13: n/a

0

Thanks for that Smeagal, as I mentioned - I know there's flaws in this code hence the title. I've tried to fix it but I can't think of a good algorithm that I can use to shorten the result and display only the relevant section, with a proper beginning of a new sentence at the start to an ending - between which is the keyword

0

Hang on a tick, i'm thinking ... so far I have :

while ($row = mysql_fetch_array($result)) {

  $begin = strpos($row['Content'], $var);
  $period = stripos($content, "."); // Swap for finding the right period?
  
  if($begin && $period) {

    $content = substr($row['Content'], $period+2, 500);

  } else {

    $content = $row['Content'];

  }

  echo "Title: $title<br>Content: $content";

}

Edited by Smeagel13: n/a

0

This works:

while ($row = mysql_fetch_array($result)) {

  $word = strpos($row['Content'], $var);
  $period = strripos(substr($row['Content'], 0, $word), ".");
  
  if($word && $period) {

    $content = substr($row['Content'], $period+2, 500);

  } else {

    // Didn't find the keyword, or there was no period before the word, what do you want to do, output everything?
    $content = $row['Content'];

  }

  echo "Title: $title<br>Content: $content";

}

Edited by Smeagel13: n/a

0

Hang on a tick, i'm thinking ... so far I have :

while ($row = mysql_fetch_array($result)) {

  $begin = strpos($row['Content'], $var);
  $period = stripos($content, "."); // Swap for finding the right period?
  
  if($begin && $period) {

    $content = substr($row['Content'], $period+2, 500);

  } else {

    $content = $row['Content'];

  }

  echo "Title: $title<br>Content: $content";

}

Thanks for your time, that seems to work a lot better but it always starts from the beginning no matter where the keyword is in the string, it just extends the 500 chars on top of the chars already. I think we're close though!

0

The above comment was about the earlier version you gave, the final one seems to work as far as I can tell. I'm going to continue testing it and see what happens.

Thanks again Smeagel!

0

No probs, for the life of me I couldn't remember what function pulls out a chunk of text... That's what I was thinking about...

Edited by Smeagel13: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.