poleposters Posted September 26, 2008 Share Posted September 26, 2008 Hi, I'm using the nl2br function to convert new lines in a text area to breaks. Is it possible to turn the breaks into paragraphs? Link to comment https://forums.phpfreaks.com/topic/126006-replacing-with-tags-from-nl2br-function/ Share on other sites More sharing options...
discomatt Posted September 26, 2008 Share Posted September 26, 2008 Code: <?php function nl2p( $str ) { $str = str_replace( array("\r\n", "\r"), "\n", $str ); return "<p>\n" . str_replace( "\n", "\n</p>\n<p>\n", $str ) . "\n</p>"; } $html = <<<HTML This is some text we want to convert line breaks to paragraph tags HTML; echo nl2p( $html ); ?> Output: <p> This is some text we want to convert </p> <p> line </p> <p> breaks </p> <p> to paragraph </p> <p> tags </p> Link to comment https://forums.phpfreaks.com/topic/126006-replacing-with-tags-from-nl2br-function/#findComment-651621 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.