views:

264

answers:

5

Say,it just returns the snippet around which the searching keyword exists.

And part of the text is replaced by "...".

Is it possible to achieve that goal with PHP and MySQL?

A: 

Can you be more specific? I have no idea what you're asking for.

Rosarch
you search something in MySQL,then MySQL returns all results that matches.But now,I just want one field of the results,and just a snippet of that field,around which the searched keyword exists,just like Google provides you when you do a search.
Shore
A: 

Probably you'll find your answer here - http://stackoverflow.com/questions/1226272/mysql-conditional-short-descriptions

Arpit Tambi
it simply cuts the first 50 characters.What I want is to locate the snippet around the part where searching keywords exists.
Shore
A: 

$snippet = "//mysql query";

function trimmer($updates,$wrds){
if(strlen($updates)<=$wrds){
return $updates;
}else{
$marker = strrpos(substr($updates,0,$wrds),' ');
$string = substr(substr($updates,0,$wrds),0,$marker)."...";return $string;

}

echo trimmer($snippet,200);//You can send the snippet string to this function it searches for the last space if string length is greater than 200 and adds "..." to it

*This is probably what you want(EDIT)

$string1="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
function trimmer($updates,$wrds,$pos){
if(strlen($updates)<=$wrds){
return $updates;
}else{
$marker = strrpos(substr($updates,$pos,$wrds),' ');
$string = substr(substr($updates,$pos,$wrds),0,$marker)."...";
 return $string;
}
}
$pos = strpos($string1, "dummy");
echo trimmer($string1,100,$pos);
halocursed
Think what if the keywords exists 201 characters from the beginning.You returned the first 200 characters,but with no keywords included.
Shore
I added an EDIT ... maybe this is what you want
halocursed
+1  A: 
function excerpt($text, $phrase, $radius = 100, $ending = "...") {
    $phraseLen = strlen($phrase);
    if ($radius < $phraseLen) {
        $radius = $phraseLen;
    }

    $pos = strpos(strtolower($text), strtolower($phrase));

    $startPos = 0;
    if ($pos > $radius) {
        $startPos = $pos - $radius;
    }

    $textLen = strlen($text);

    $endPos = $pos + $phraseLen + $radius;
    if ($endPos >= $textLen) {
        $endPos = $textLen;
    }

    $excerpt = substr($text, $startPos, $endPos - $startPos);
    if ($startPos != 0) {
        $excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
    }

    if ($endPos != $textLen) {
        $excerpt = substr_replace($excerpt, $ending, -$phraseLen);
    }

    return $excerpt;
}

Shamelessly stolen from the Cake TextHelper.

deceze
A: 

Modified deceze's function slightly to allow multiple phrases. e.g. your phrase can be "testa testb" and if it does not find testa, then it will go to testb.

function excerpt($text, $phrase, $radius = 100, $ending = "...") { 


         $phraseLen = strlen($phrase); 
       if ($radius < $phraseLen) { 
             $radius = $phraseLen; 
         } 

      $phrases = explode (' ',$phrase);

      foreach ($phrases as $phrase) {
       $pos = strpos(strtolower($text), strtolower($phrase)); 
       if ($pos > -1) break;
      }

         $startPos = 0; 
         if ($pos > $radius) { 
             $startPos = $pos - $radius; 
         } 

         $textLen = strlen($text); 

         $endPos = $pos + $phraseLen + $radius; 
         if ($endPos >= $textLen) { 
             $endPos = $textLen; 
         } 

         $excerpt = substr($text, $startPos, $endPos - $startPos); 
         if ($startPos != 0) { 
             $excerpt = substr_replace($excerpt, $ending, 0, $phraseLen); 
         } 

         if ($endPos != $textLen) { 
             $excerpt = substr_replace($excerpt, $ending, -$phraseLen); 
         } 

         return $excerpt; 
   }

Highlight function

function highlight($c,$q){ 
$q=explode(' ',str_replace(array('','\\','+','*','?','[','^',']','$','(',')','{','}','=','!','<','>','|',':','#','-','_'),'',$q));
for($i=0;$i<sizeOf($q);$i++) 
    $c=preg_replace("/($q[$i])(?![^<]*>)/i","<span class=\"highlight\">\${1}</span>",$c);
return $c;}
Alec Smart
Can you highlight the phrases?
Shore
added the highlight function...
Alec Smart