PHP Function to Convert LineBreaks to NewLines
June 15th, 2007 –
Category: Code Snippets –
No Comments »
While PHP has a very nice little function (nl2br) to convert newlines (\n) to line breaks (<br>), moving in the opposite direction is not quite so easy. This function should hopefully help to allow you to convert line breaks to new lines.
<?php
/* This function will convert line breaks or other tags passed in the $tags variable
to linebreaks. Multiple $tags must be separated by spaces, and must consist of the
regular tag text. Ie. $result = br2nl($text_to_filter, "br p blockquote") */
function br2nl($text, $tags = "br")
{
$tags = explode(" ", $tags);
foreach($tags as $tag)
{
$text = eregi_replace("<" . $tag . "[^>]*>", "\n", $text);
$text = eregi_replace("</" . $tag . "[^>]*>", "\n", $text);
}
return($text);
}
// Usage:
$text_to_filter = "<p>This is my <br>sample<br>text. The default code listed here " .
"should replace the br's with new lines.</p><p>The second example is more advanced, " .
"stripping out both the BR's as well as the P tags.</p>";
// Example of replacing BR tags (default)
$result = br2nl($text_to_filter);
echo "<pre>$result</pre>";
// Example of replacing both BR and P tags
$result = br2nl($text_to_filter, "br p");
echo "<pre>$result</pre>";
?>
Related Reading:
Build Your Own PHP FrameworkThere's a lot of different PHP frameworks out there. Some are big some, are small. But I doubt they do things exactly the way you want it.... Read More >
Beginning PHP and MySQL: From Novice to ProfessionalBeginning PHP and MySQL: From Novice to Professional, Fourth Edition is a major update of W. Jason Gilmore's authoritative book on PHP and MySQL. T... Read More >
PHP for the Web: Visual QuickStart Guide (4th Edition)With PHP for the World Wide Web, Fourth Edition: Visual QuickStart Guide, readers can start from the beginning to get a tour of the programming langua... Read More >
Feedback
Related Entries
Links:
- Web Hosting Canada hosting packages starts at $3.95/m. 24/7 toll free support.
Recommended:
Other Information:
Programming Related
Articles we've written related to the topic of PHP Programming.
- AmazonFeed 2.1 Released
(April 13th, 2011 – 7 Comments) - Beware of Thieves like AmazonPress
(April 22nd, 2010 – 7 Comments) - Paginated Navigation Bar
(December 21st, 2009 – 2 Comments) - Wicked Cool PHP – Review
(November 23rd, 2009 – One Comment) - PHP Object Unit Testing
(June 5th, 2009 – Comment on This)
Website Development Tips
Tips and strategies related to the development of great websites.
- Aptana and Eclipse Mouse Click Problems
(January 21st, 2010 – One Comment) - Captcha Insanity?
(June 18th, 2009 – One Comment) - PHP Object Unit Testing
(June 5th, 2009 – Comment on This) - FormContact 1.1 with PHP5 Support
(June 24th, 2008 – 3 Comments) - Website Subversion Backup
(April 8th, 2008 – One Comment)
General Information & Resources
General information and resources from WarkenSoft Productions.
- Beware of Thieves like AmazonPress
(April 22nd, 2010 – 7 Comments) - Aptana and Eclipse Mouse Click Problems
(January 21st, 2010 – One Comment) - Wicked Cool PHP – Review
(November 23rd, 2009 – One Comment) - New Webmaster Tool – The Regular Expression Editor
(June 15th, 2007 – Comment on This) - Where to Start with a Website
(March 19th, 2007 – One Comment)


