Click to See Complete Forum and Search --> : Ending a string after so many words, check it out :-D


TimTimTimma
07-18-2005, 06:27 PM
There seemed to be a deal of users interested in ending strings after so many WORDS, and not CHARCTERS, so i decided to wipe up this script :-)


<?php

// String we will be using to break up into literal words

$string = "Mommie, wow! I'm a big kid now!";

// Maximum amount of words

$max = 5;

// lets trim the beggining and end of the string to make sure
// there are no spaces that will create 'empty' words in the
// next step

$string = trim($string);

// lets make words out of this string!

$word = explode(" ", $string);

//now to count how many wods we have (7)

$total_words = count($word);

// now to see if there are to many, and if there are display
// only up to the maximum allowed

if($total_words > $max){
for($i = 0; $i <= $max; $i++){
if($i == $max){
echo $word[$max]."....";

// this is the cut off point, the rest
// of the string is replaced with
// trailing periods "...."
}else{
echo $word[$i]." ";

// as long as the variable $i is not
// equal to the maximum words allowed
// each word will trail with a space
// up until th max limit is reached
}
}
}else{
echo $string;

// the string does not contain more words than the
// maximum allowed, so display the entire string
}
?>


Fairly simple, fast, and straight to the point, I think its pretty good, what do you think?

thorpe
07-18-2005, 06:53 PM
nice functional piece of code. just to clean this up a little, why not put it in a function?

function limitwords($string,$max,$trail='')
{
$max--;
$tmp = '';
$string = trim($string);
$word = explode(" ", $string);
$total_words = count($word);
if ($total_words > $max) {
for ($i = 0; $i <= $max; $i++) {
if ($i == $max) {
$tmp .= $word[$max].$trail;
} else {
$tmp .= $word[$i]." ";
}
}
} else {
$tmp = $string;
}
return $tmp;
}

ive also fixed an error where it seems you forgot that array indexes start at 0 (notice the $max--;), i also thought it would be a good idea to have the trail as an argument. this way, you could easily put a link or something in there. eg;

echo limitwords('here is a small snippet of an article',4,'<a href="more.php?id=2">read more</a>');

TimTimTimma
07-18-2005, 07:20 PM
nice functional piece of code. just to clean this up a little, why not put it in a function?

function limitwords($string,$max,$trail='')
{
$max--;
$tmp = '';
$string = trim($string);
$word = explode(" ", $string);
$total_words = count($word);
if ($total_words > $max) {
for ($i = 0; $i <= $max; $i++) {
if ($i == $max) {
$tmp .= $word[$max].$trail;
} else {
$tmp .= $word[$i]." ";
}
}
} else {
$tmp = $string;
}
return $tmp;
}

ive also fixed an error where it seems you forgot that array indexes start at 0 (notice the $max--;), i also thought it would be a good idea to have the trail as an argument. this way, you could easily put a link or something in there. eg;

echo limitwords('here is a small snippet of an article',4,'<a href="more.php?id=2">read more</a>');



nope I was aware, thats why $i starts at 0 instead of 1 in the for loop

P.S. I like your modification :-)

Weedpacket
07-19-2005, 02:33 AM
Have you seen explode's limit parameter?