-2

A have something like this. It works fine, but I have been tried 2 hours to cut search result - 100 str before $word and 100 after. How can I make this?

$word = $_POST["search"];

$sql_events = mysql_query("SELECT * FROM events WHERE event_title LIKE '%" . $word . "%' OR event_desc LIKE '%" . $word ."%'");

function highlightWords($content, $word){
    if(is_array($search)){
        foreach ( $search as $word ){
            $content = str_ireplace($word1, '<span>'.$word1.'</span>', $content);
        }
    } else {
        $content = str_ireplace($word, '<span>'.$word.'</span>', $content);     
    }
    return $content;
}
while($row = mysql_fetch_array($sql_events)){

    $content = $row["event_desc"];

    $result = highlightWords($content, $word);
    echo '<li>
            <H3><a href="">'.$row['event_title'].'</a></H3>
            <div class="text">'.$result .'...</div>
         </li>';
}
Al.G.
  • 4,327
  • 6
  • 31
  • 56
IFier
  • 98
  • 2
  • 9
  • 2
    What do you want exactly? I do not understand your question... – RichardBernards Nov 21 '14 at 11:23
  • I guess OP wants to cut the results so only the 100 characters before and after `$word` are shown to the user (instead of the whole document). What have you tried? Can you show your most promising attempt(s)? – kero Nov 21 '14 at 11:24
  • I suppose this is the string they want to cut - `$content = $row["event_desc"];`. Am I right? – Al.G. Nov 21 '14 at 11:24
  • `$content = $row["event_desc"]` - yes. But cut it around search word. I tried this: `preg_replace("/^.*?(.{0,100})\b($word)\b(.{0,100}).*?$/i", '\1\2\3', $text);` – IFier Nov 21 '14 at 11:29
  • Obligatory [How to prevent SQL injections](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) – SubjectCurio Nov 21 '14 at 11:30
  • @RichardBernards - I need to get a part of the string and only the whole string. A scenario is to get 100 chars before and 100 chars after the first match. Any help will be appreciated. – IFier Nov 21 '14 at 11:33

2 Answers2

0

You need this regular expression:

[\d\D]{0,100}word[\d\D]{0,100}

Where word is the main word :). Here it is implemeted with your code:

$content = $row["event_desc"];
$word_in_regexp = quotemeta($content);
preg_match("/[\d\D]{0,100}$word_in_regexp[\d\D]{0,100}/i", $content, $matches);
$content = $matches[0];

We use quotemeta() in case $content contains characters like [, (, \ etc.

Al.G.
  • 4,327
  • 6
  • 31
  • 56
  • After this, browser understands all PHP code like text. If I change this `"/[\d\D]{0,100}$word_in_regexp[\d\D]{0,100}/"` to `$word_in_regexp`, PHP code works, it isn't echo result values at all. – IFier Nov 21 '14 at 11:47
  • It's seems like WP don't like regular expression. – IFier Nov 21 '14 at 12:08
0
$wordpos1 = (mb_strpos($content, $word) - 100);
  if ($wordpos1 < 0){
    $wordpos1 = 0;
  } else {
    $wordpos1 = (mb_strpos($content, $word) - 100);
  }
  $wordpos2 = (mb_strpos($content, $word) + 400);

  $resText = mb_substr($content, $wordpos1, $wordpos2);

  $result = highlightWords($resText, $word);

And it's works to me :)

IFier
  • 98
  • 2
  • 9