简体   繁体   中英

Limit text words without cut last word

Im showing a text excerpt that starts with a searched word with more 35 chars after that searched word.

Do you know some way to show this text excerpt (searched word + 35chars) without cut the last word, because with substr is not working?

$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>';

I'm guessing you are looking for something like the following:

<?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);

which gives string(33) "Do you know some way to show this"

Please use something like this:

//$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);

It's clear way to do it.
I hope that's a behavior what you meant.
You can check it at: http://writecodeonline.com/php

There are several "kinds" of text you can pass into that:

Text where searched word isn't present
=> return NULL:

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

Text with spaces longer than desired excerpt length (searched word length + eg 35)
=> return org text cut out but keep whole last word:

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

Text with spaces equal to desired excerpt length
=> return org text:

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

Text with spaces shorter than desired excerpt length
=> return org text:

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

Text without spaces longer than desired excerpt length
=> return org text:

"This_text_has_no_spaces_and_is_really_longer" => "This_text_has_no_spaces_and_is_really_longer"

Text without spaces equal to desired excerpt length
=> return org text:

"This_text_has_no_spaces_and_is_the_same" => "This_text_has_no_spaces_and_is_the_same"

Text without spaces shorter than desired excerpt length
=> return org text:

"This_text_has_no_spaces_and_is_shorter" => "This_text_has_no_spaces_and_is_shorter"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM