WordPress.org

Ready to get started?Download WordPress

Forums

Postie
Request: strip newlines but treat newlines+newlines as paragraph (2 posts)

  1. humptybump
    Member
    Posted 1 year ago #

    I'm still struggling to get plain test emails to post correctly. I've tried filtering newlines but that runs all my text together. I've tried not filtering newlines but that breaks text at the plane text email per-define line length.

    I've decided what would be ideal is to treat newline different from newline+newline. The relevant code is here:

    //filter content for new lines
    function filter_Newlines(&$content, $config) {
        if ($config['filternewlines']) {
    
            $search = array(
                "/\r\n/",
                "/\n\n/",
                "/\r\n\r\n/",
                "/\r/",
                "/\n/"
            );
            $replace = array(
                "LINEBREAK",
                "LINEBREAK",
                'LINEBREAK',
                'LINEBREAK',
                'LINEBREAK'
            );
    
            $result = preg_replace($search, $replace, $content);
            if ($config['convertnewline']) {
                $content = preg_replace('/(LINEBREAK)/', "<br />\n", $result);
            } else {
                $content = preg_replace('/(LINEBREAK)/', " ", $result);
            }
        }
    }

    Temporarily I plan to change this code so it will FILTER but not CONVERT instances of single newline, but leave double newline instances.

    http://wordpress.org/extend/plugins/postie/

  2. humptybump
    Member
    Posted 1 year ago #

    Here is the change that is working for me:

    function filter_Newlines(&$content, $config) {
        if ($config['filternewlines']) {
            $search1 = array(
                "/\r\n/",
                "/\r/",
                "/\n/"
            );
            $search2 = array(
                "/\n\n/",
                "/\r\n\r\n/"
            );
            $replace1 = array(
                'LINEBREAK',
                'LINEBREAK',
                'LINEBREAK'
            );
            $replace2 = array(
                'DOUBLEBREAK',
                'DOUBLEBREAK'
            );
    
            $result = preg_replace($search2, $replace2, $content);	// must replace double first
            $result = preg_replace($search1, $replace1, $result);
    
            if ($config['convertnewline']) {
                $result = preg_replace('/(LINEBREAK)/', "<br />\n", $result);
                $content = preg_replace('/(DOUBLEBREAK)/', "<br />\n<br />\n", $result);
            } else {
                $result = preg_replace('/(LINEBREAK)/', " ", $result);
                $content = preg_replace('/(DOUBLEBREAK)/', "\r\n\r\n", $result);
            }
        }
    }

    As the comment notes, the key is to replace all double breaks first, then all single breaks.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic