GizzmoAsus.co.uk

Convert Linebreaks to New Lines

by 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(']*>', '\n', $text); 
    } 
    return($text); 
}

// Usage: $text_to_filter = '

This is my
sample
text. 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 

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
Convert Linebreaks to New Lines, 5.0 out of 5 based on 1 rating

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!