6
A
回答
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;}
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「
相關問題
- 1. 在php中生成代碼片段
- 2. 使用php生成html或javascript代碼片段代碼
- 3. GoodRelations的RDFa代碼片段生成器
- 4. 如何使用jooq代碼生成器與maven生成代碼
- 5. 如何使用寧靜代碼生成器生成代碼(.ts)
- 6. 插入有界代碼片段到生成的Google Spreadsheets
- 7. 使用Easy PHP2WSDL生成的PHP代碼
- 8. 如何用php和mysql生成md5
- 9. 生成不生成代碼
- 10. 如何設計生成Python代碼的代碼生成器
- 11. 爲php和html代碼生成簡碼
- 12. PHP代碼不會生成PHP代碼
- 13. 使用php生成qr代碼
- 14. 使用Scala生成(PHP-)代碼
- 15. 如何從JQuery上通過PHP和mySQL生成Chart.js圖生成?
- 16. xtext代碼生成,如何避免代碼生成一行?
- 17. 生成php代碼爲pdf
- 18. 使用EdmGen生成ssdl和csdl但不生成代碼?
- 19. 使用EMF生成代碼
- 20. 使用Ant生成代碼
- 21. 使用.net生成代碼
- 22. 使用Jcodemodel代碼生成
- 23. 使用yacc代碼生成
- 24. 使用Scala代碼生成
- 25. 生成JSON不使用PHP和MySQL
- 26. 如何生成c#代碼?
- 27. 如何編寫代碼片段以在C#中生成方法?
- 28. 如何從現有代碼生成片段?
- 29. 如何生成一個jQuery代碼片段
- 30. 代碼生成
這些被稱爲省略號en.wikipedia.org/wiki/Ellipsis – Strawberry 2014-04-27 23:12:09