Ollie
{ MODERATOR }
posts: 8
last: 26-Mar-2008
TITLE: Paragraphize
DESCRIPTION: Adds html paragraph codes around paragraphs
Submitted: 23-Nov-2007 22:22:38 ( 1yrs 6w 3d 12h ago ) Language: PHP (*.php *.php4 *.php5 *.phtml)
Views: 424 Lines of Code: 19 LINES
Rating:
rate: star1
star2
star3
star4
star5
dstar1
dstar2
dstar3
dstar4
dstar5  ( rated! )
  { 4.00 / 5 }
Difficulty: Intermediate
Bookmark
/* Author: Ollie
   Date: 23-11-2007
*/


// CODE  
function paragraphize($text){
	return nl2br(preg_replace('/\\n?(.+?)(?:\\n\\s*\\n|\\z)/s', '<p>\\1</p>', $text));
}

// Example

$paragraphtext = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. 

 Cras ante sem, aliquet eget, augue. Donec massa dui, sodales eget, ornare in, porta eget, nisl. Phasellus volutpat commodo lectus. Fusce sed arcu. 

 Fusce lectus elit, eleifend ut, convallis in, feugiat eu, justo. Pellentesque hendrerit sem scelerisque felis. Morbi hendrerit tellus a nunc. ';

print(paragraphize($paragraphtext));