Forum Moderators: coopster

Message Too Old, No Replies

Snip at end of a sentence

How to?

         

abbeyvet

12:52 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually I want to snip text at the end of the second sentence.

I am currently snipping after 145 characters, but really mean "go about 140 characters in then snip at the next full stop" (full stop=period for US readers!)

This is what I am using now:

function snip($entry) {

$chars = 145;
$entry = $entry." ";
$entry = substr($entry,0,$chars);
$entry = substr($entry,0,strrpos($entry,' '));
$entry = $entry."...";

return $entry;

}
echo snip$entry;

Can this be done?

justageek

3:44 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps like this:

function snip($entry) {

$chars = 140;
$entry = $entry." ";
$entry = substr($entry,$chars);
$entry = substr($entry,0,strrpos($entry,'.'));
$entry = $entry."...";

return $entry;

}

JAG

mcibor

5:03 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's not so nice, as it will cut after the first dot, not the dot after 140th char.

I would change the function to

function snip($entry) {

$chars = 140;
$entry = substr($entry,0,strrpos($entry,'. ', $chars));
$entry = $entry."...";

return $entry;

}

Regards
Michal

abbeyvet

5:09 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you VERY much, that's perfect! :)

mooger35

7:26 pm on Dec 13, 2006 (gmt 0)

10+ Year Member



shouldn't it be:

function snip($entry) {

$chars = 140;
$entry = substr($entry,0,strpos($entry,'. ', $chars));
$entry = $entry."...";

return $entry;

}

aren't you looking for the first "full stop" after 140 characters?

strrpos would be the last occurence where strpos is the first. Or am I incorrect in what you want?