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!

Recommended Answers

All 8 Replies

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.

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

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";

}

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";

}

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!

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!

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

Haha. At least you got there in the end.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.