Forum Moderators: coopster

Message Too Old, No Replies

How to avoid words being chopped with SUBSTR

         

hafnius

9:59 pm on Apr 12, 2004 (gmt 0)

10+ Year Member



Hi all

A small problem

I am working on an article DB. on the first page i show the first 180 char. of each and a link to the hole article. This works fine:


if ((strlen($body) > 180) && (strlen($body) > 1)) { $body = substr("$body", 0, 180)."... ";}

The problem is that the 180 char. limit chops of words. I have tried to take a look at PHP's numeruos string functions but havent been able to make it so that the words are not chopped of. Maybe it is possible to retrieve a set number of words instead of char.

I hope the prob is clear.

Kind regards
/Klaus b

jatar_k

10:03 pm on Apr 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about using strrchr [ca.php.net] to find the last occurrence of space?

Should work to chop off the end of the string after the last space.

Birdman

10:13 pm on Apr 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First find the first occurance of a whitespace starting from, say the 175th character. Then use that position as the cutoff point. Also, no quotes needed around the $body variable.

if ((strlen($body) > 180) && (strlen($body) > 1)) {
$whitespaceposition = strpos($body," ",175)-1;
$body = substr($body, 0, $whitespaceposition);
}

Hope it helps

Birdman

hafnius

10:46 pm on Apr 12, 2004 (gmt 0)

10+ Year Member



Hi Jatar K and Birdman

Of course, you are both right - really a smart solution to look for whitespace. The solution you provided Birdman, worked like a charm.

Thanx :)

/Kind Regards
Klaus b

coopster

11:06 pm on Apr 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Another option is to use a function for this type of need. It seems to come up now and again, so I'll share what I have. Maybe we have some additional input to make it even better...
function [php.net] truncate($string, $length = '', $replacement = '...', $start = 180) { 
if [php.net] (strlen [php.net]($string) <= $start) return [php.net] $string;
if [php.net] ($length) {
return [php.net] substr_replace [php.net]($string, $replacement, $start, $length);
} else [php.net] {
return [php.net] substr_replace [php.net]($string, $replacement, $start);
}
}
// ...and in your processing code...
$body = truncate($body);

jackson

2:11 am on Apr 28, 2004 (gmt 0)

10+ Year Member



coopster

Hi - managed to implement your piece of code quite successfully. I've added in a [read more] link back to the complete body of orginal text.

On long strings (I've set mine to 60 characters) it works as intended. However, on shorter strings (less than 60 characters), the code seems to throw in a line break somewhere. This between the end of the truncated text block and the [read more] link. Any ideas on how to may be resolve this?

Not sure if this is actually coming in from the original piece of text as found in the $body item?

coopster

2:21 pm on Apr 28, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It may be something in the variable that is causing your line breaks. Otherwise it may be something in your stylesheet causing the
a
element to act as something other than an inline element. Try viewing the source of your output html code to determine the culprit.