限制文字而不删除最后一个单词

时间:2014-09-04 05:56:00

标签: php

我正在显示一个文本摘录,以搜索到的单词后面的搜索词开头,后面有更多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>';

2 个答案:

答案 0 :(得分: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 :(得分: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

你可以传达几种“种类”的文字:

搜索词不存在的文字
=&gt; return NULL:

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

文本的空格长于所需的摘录长度(搜索的字长+例如35)
=&gt;返回组织文本删除但保留完整的最后一句话:

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

空格等于所需摘录长度的文字
=&gt;返回组织文本:

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

空格短于所需摘录长度的文字
=&gt;返回组织文本:

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

没有空格长于所需摘录长度的文字
=&gt;返回组织文本:

"This_text_has_no_spaces_and_is_really_longer" => "This_text_has_no_spaces_and_is_really_longer"

没有空格的文本等于所需的摘录长度
=&gt;返回组织文本:

"This_text_has_no_spaces_and_is_the_same" => "This_text_has_no_spaces_and_is_the_same"

没有空格的文本短于所需的摘录长度
=&gt;返回组织文本:

"This_text_has_no_spaces_and_is_shorter" => "This_text_has_no_spaces_and_is_shorter"