Convert Linebreaks to New Lines
by GizzmoAsus on Dec.21, 2007, under PHP Code
Intro
During my development process I am forever using the same information for both text-based emails and html-based emails. Then a thought came accross me, why not just use the same information and replace the
with \n using some form of regular expressions. This lead to me doing a quick search on the net to ensure I wasn’t reinventing the wheel and I came accross the following:
Code
/* 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("e; "e;, $tags);
foreach($tags as $tag) {
$text = eregi_replace('<' . $tag . '[^>]*>', '\n', $text);
$text = eregi_replace('' . $tag . '[^>]*>', '\n', $text);
}
return($text);
}
// Usage: $text_to_filter = '
This is my sampletext. The default code listed here should replace the br's with new lines.
The second example is more advanced, stripping out both the BR's as well as the P tags.
';
// Example of replacing BR tags (default)
$result = br2nl($text_to_filter);
// Example of replacing both BR and P tags $result = br2nl($text_to_filter, "br p");
?>
Function Found at http://www.warkensoft.comÂ