2014-09-04 20 views
0

我顯示了一個文本摘錄,該搜索結果以搜索到的單詞後面帶有更多35個字符的搜索詞開頭。限制沒有剪輯結束詞的文本字

你知道一些方法來顯示這段文字摘錄(搜索詞+ 35chars)沒有削減硬道理,因爲substr不工作?

$search = $url[1]; 
$read = $pdo->prepare("SELECT * FROM pages WHERE title LIKE ? OR content LIKE ? LIMIT ?,?"); 
$read->bindValue(1, "%$search%", PDO::PARAM_STR); 
$read->bindValue(2, "%$search%", PDO::PARAM_STR); 
$read->bindParam(3, $begin,PDO::PARAM_INT); 
$read->bindParam(4, $max,PDO::PARAM_INT); 
$read->execute(); 
$searchPos = stripos($result['content'],$search); 
$searchLen = strlen($search); 
$result_text = '"'.substr($result['content'], $searchPos, $searchLen + 35).'..."'; 
echo '<p>'.strip_tags($result_text).'</p>'; 
+0

那就是'substr'做,切東西,你告訴它。 – Prix 2014-09-04 06:00:25

+0

什麼是電流輸出和所需的輸出? 「不工作」還不夠清楚。 – Sugar 2014-09-04 06:11:24

+0

問題是輸出總是動態的! – OzzC 2014-09-04 06:17:10

回答

1

我猜你正在尋找的東西像下面這樣:

<?php 
#Do you know some way to show this text excerpt (searched word + 35chars) without cut the last word? 
$search = 'Do'; 
$str = explode(' ', 'you know some way to show this text excerpt (searched word + 35chars) without cut the last word?'); 
$len = strlen($search) + 1;#might need to be 0 if you don\'t want search to be with the 35 characters 
$ret = $search . ' '; 
foreach($str as $st){ 
    if(strlen($st) + $len < 35){ 
     $len += strlen($st) + 1; 
     $ret .= $st . ' '; 
    }else{ 
     break; 
    } 
} 
$ret = trim($ret); 
var_dump($ret); 

這給string(33) "Do you know some way to show this"

1

請使用這樣的事情:

//$orgText = "This text is exactly the same length..."; 
//$orgText = "This text is shorter..."; 
//$orgText = "This_text_has_no_spaces_and_is_shorter"; 
//$orgText = "This_text_has_no_spaces_and_is_the_same"; 
//$orgText = "This_text_has_no_spaces_and_is_really_longer"; 

$orgText = "This text is longer and will be definitely cut but last word survives..."; 

$searchedWord = "This"; 
$charsToShow = 35; 
$desiredExcerptLength = strlen($searchedWord) + $charsToShow; 

$searchedWordPos = strpos($orgText, $searchedWord); 

if ($searchedWordPos !== false) { // Word found 
    $subText = substr($orgText, $searchedWordPos); // Subtext: begins with searched word and ends at org text end 
    $subTextLength = strlen($subText); 

    if ($subTextLength > $desiredExcerptLength) { // Subtext longer than desired excerpt => cut it but keep last word 
     $spaceAfterLastWordPos = strpos($subText, ' ', $desiredExcerptLength); 
     $excerpt = substr($subText, 0, $spaceAfterLastWordPos ? $spaceAfterLastWordPos : $subTextLength); 
    } 
    else { // Subtext equal or shorter than desired excerpt => keep all text 
     $excerpt = $subText; 
    } 
} 

var_dump($excerpt); 

很明顯的方式去做吧。
我希望這是你的意思。
你可以檢查一下:http://writecodeonline.com/php

有幾種文字的 「種」,您可以通過成:

Text中搜索詞不存在
=>返回NULL:

input: NULL, "", "Something without searched word"=> result: NULL 

與空間比預期的摘錄長度較長的文本(搜索字長+例如35)
=>返回組織文本切出,但保持整個最後一個字:

"This text is longer and will be definitely cut but last word survives..." => "This text is longer and will be definitely" 

用空格等於期望的摘錄長度的文本
=>返回的有機文本:

"This text is exactly the same length..." => "This text is exactly the same length..." 

文本用空格比期望的摘錄長度短
=>返回的有機文本:

"This text is shorter..." => "This text is shorter..." 

文本沒有空格長於期望的摘錄長度
=>返回的有機文本:

"This_text_has_no_spaces_and_is_really_longer" => "This_text_has_no_spaces_and_is_really_longer" 

文本沒有空格等於期望的摘錄長度
=>返回組織文本:

"This_text_has_no_spaces_and_is_the_same" => "This_text_has_no_spaces_and_is_the_same" 

文本沒有空格比預期的摘錄長度較短
=>返回組織文本:

"This_text_has_no_spaces_and_is_shorter" => "This_text_has_no_spaces_and_is_shorter"