2009-08-18 145 views

回答

0
$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 

這可能是你想要什麼(編輯):

$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); 
+0

想想看,如果關鍵字從一開始就存在201個字符。 您返回了前200個字符,但沒有包含任何關鍵字。 – omg 2009-08-18 06:45:31

+0

我加了一個編輯...也許這就是你想要的 – halocursed 2009-08-18 07:27:17

4
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; 
} 

Cake TextHelper無恥被盜。

8

修改deceze的函數略有許多短語。例如你的短語可以是「testa testb」,如果它沒有找到testa,那麼它會去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; 
    } 

高亮顯示功能

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;} 
+0

你能突出顯示這些短語嗎? – omg 2009-09-17 02:04:52

+1

添加了高亮功能... – 2009-09-18 07:32:26

+1

這應該是接受的答案 – Kamal 2013-01-24 10:57:19

5

我給多個多個關鍵字和多次出現溶液(也適用於修飾不區分大小寫):

function excerpt($text, $query) 
{ 
//words 
$words = join('|', explode(' ', preg_quote($query))); 

//lookahead/behind assertions ensures cut between words 
$s = '\s\x00-/:[email protected]\[-`{-~'; //character set for start/end of words 
preg_match_all('#(?<=['.$s.']).{1,30}(('.$words.').{1,30})+(?=['.$s.'])#uis', $text, $matches, PREG_SET_ORDER); 

//delimiter between occurences 
$results = array(); 
foreach($matches as $line) { 
    $results[] = htmlspecialchars($line[0], 0, 'UTF-8'); 
} 
$result = join(' <b>(...)</b> ', $results); 

//highlight 
$result = preg_replace('#'.$words.'#iu', "<span class=\"highlight\">\$0</span>", $result); 

return $result; 
} 

這是例如導致查詢=「什維霍夫prohlídkám「

Result

+0

fyi-如果$ text中包含$ query的行在其之前和之後沒有換行符,則這看起來不匹配。 $ text =「\ n $ {text} \ n」是一個簡單的解決方法,但正則表達式可能需要調整 – dlo 2016-12-22 00:10:05

+0

如果您有長文本和很多匹配,這將返回很長的摘錄。 – jor 2017-02-12 22:18:12

+0

棒極了!謝謝。 – 2017-06-14 08:53:54