Re: Truncate words in multiple paragraphs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi :)

I am using the following function (that I found in the user comments on php.net manual) for trimming the number of words accepted via a form field

// Truncation is by word limit, not character limit. So if you limit
// to 255, then it will cut off a text item to 255 words, not 255 characters
function trim_text ($string, $truncation=250) {
	$string = preg_split("/\s+/",$string,($truncation+1));
	unset($string[(sizeof($string)-1)]);
	return implode(' ',$string);
}

This works well, except it is also removing any line breaks. For instance...

-snip-

I'd like to (need to) retain the line breaks. Any suggestions?

Thanks,
verdon


verdon:

I had the same problem and fixed it with this:

// remove all linefeeds and replace with new paragraph
$pattern = "\n";
$replace = "</p><p>";
$des = eregi_replace($pattern, $replace, $des);

Then in your html, use:

<p><?php echo($des); ?></p>

Worked for me.

tedd


--
--------------------------------------------------------------------------------
http://sperling.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux