Forum Moderators: coopster

Message Too Old, No Replies

Retrieving certain numbers of characters from string

But wont truncate the last word if it falls on the middle of the last word.

         

Gian04

12:01 pm on Jul 4, 2007 (gmt 0)

10+ Year Member



I want to get the first 100 characters using mb_strimwidth [php.net]. But I dont want to truncate the last word if it falls on the middle of the last word.

Example :

The quick brown fox jump over the lazy dog

Getting the 1st 22 characters will display

The quick brown fox j...

But I want to always display the full last word

The quick brown fox jump...

How can I do that?

Thanks!

Habtom

1:10 pm on Jul 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Tested and works:

$str = "The quick brown fox jump over the lazy dog ";
$i = 0; $count = 0;
$exp = explode(" ", $str);
while ($count < 18) {
$an_val = $exp[$i];
$output .= $exp[$i] ." ";
$count = $count + strlen($an_val);
$i++;
}
echo $output;

But again, there is always better way of doing things.

Habtom