Page 1 of 1

Condensing Text to a number of lines.

Posted: Wed Nov 01, 2006 4:28 pm
by wesohaire
Hey guys, I'm doing a site and have some paragraphs that I need condensed down to a specific number of lines. Is this even possible? I tried substr but that is inconsistent and sometimes cuts off in the middle of a word. I've also tried exploding the string and outputting just a specific number of words but that I still need more control since say, 10 words will sometimes break down to 2 lines and sometimes fit on one line. Here is what I'm trying to do:

http://www.whatiscore.com/images/tempAsk.jpg

Here is the code I've created for the explode:

Code: Select all

<?

function constrain ($str,$n)
{ 
	$maxWords =$n-1;
	$words = explode(" ", $str);
	
	
	for ($i=0; $i<=$maxWords; $i++)
	{
	  echo "$words[$i] ";
	}
}
$par = "non nisi. Suspendisse metus dui, fermentum dignissim, vehicula et, viverra vitae, enim. Pellentesque at arcu. Praesent quis sem id turpis tempor dictum. Nulla aliquam ipsum et augue. Curabitur odio nibh, rhoncus sit amet, semper non, vehicula vel, neque. Sed nisi quam, eleifend rutrum, convallis id, adipiscing ut, lectus. Vivamus placerat aliquam tortor. Morbi interdum, metus at tincidunt ultricies, nisi sem sollicitudin risus, eget congue tellus pede nec ante. Maecenas vitae enim ut ligula egestas fermentum. Sed eu felis vel ipsum fermentum auctor. Duis gravida congue magna. Duis urna. Praesent eget turpis. Nunc tempor ornare elit. Mauris fermentum risus ut mauris. Praesent id nisi sed massa varius facilisisl";

constrain($par, 20);

?>




Thanks for all of the help!

Posted: Wed Nov 01, 2006 4:34 pm
by Luke
you need to find out how many characters fit on one line... let's say that 200 fit on one line (just for the sake of argument)... so now you need to find the 400th character, and if it's in the middle of a word, chop the word off and there ya go.

Posted: Wed Nov 01, 2006 5:34 pm
by feyd
It may be of interest to look at a thread linked from Useful Posts.