Fixed Suggestion for reply-email text handling

ccb

New member
I have been working with the watched_thread_reply_messagetext email template for ign.com/boards. We are running the snippet helper to shorten and clean the text sent out in email as follows:
{xen:helper snippet, $reply.message, 30, {xen:array 'fromStart=1', 'stripQuote=1'}}

After compilation, this runs through XenForo_Helper_String::bbCodeStrip(), where handling of the stripQuote option is "extremely primitive". This was giving us messy output when the message begins with nested quotes. We would see portions of quoted text and unbalanced [/quote] tags. Here is a suggestion for "if($stripQuote)..." in bbCodeStrip(), tested against a few hundred of our board posts.

// The following block reimplements XenForo's $stripQuote behavior. The original did
// an ungreedy regex on spans of
tags, resulting in orphaned quotation fragments
// and unbalanced
tags when quotes were nested. Here we attempt to deal
// hierarchically with nested quotes and multiple quote sections in the text.
if ( $stripQuote ) {
// Break on every start- or end-quote tag and work from there.
$chunks = preg_split( '{(\[quote[^\]]*\]|\[/quote\])}si', $string, NULL, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
if ( sizeof( $chunks ) > 1 ) {
$string = '';
$quoteLevel = 0;
$hadQuotes = false;
foreach ( $chunks as $chunk ) {
if ( preg_match( '{^\[quote}i', $chunk ) ) {
$quoteLevel += 1;
$string .= "\n";
$hadQuotes = true;
} elseif ( preg_match( '{^\[/quote}i', $chunk ) ) {
if ( $quoteLevel > 0 ) {
$quoteLevel -= 1;
}
$string .= "\n";
$hadQuotes = true;
} else {
if ( $quoteLevel == 0 ) {
$string .= $chunk;
}
}
}
// This part is optional, but it tends to improve the text.
if ( $hadQuotes ) {
// Quotes are typically found at the start of the message.
$string = preg_replace( '/^\n+/', '', $string );
// We inserted some newlines, so let's clean up any long runs.
$string = preg_replace( '/\n\n\n+/', "\n\n", $string );
}
}
}
 
OK, this will come out better. Great code deserves great formatting.

PHP:
// The following block reimplements XenForo's $stripQuote behavior. The original did
// an ungreedy regex on spans of [quote] tags, resulting in orphaned quotation fragments
// and unbalanced [/quote] tags when quotes were nested. Here we attempt to deal
// hierarchically with nested quotes and multiple quote sections in the text.
if ( $stripQuote ) {
    // Break on every start- or end-quote tag and work from there.
    $chunks = preg_split( '{(\[quote[^\]]*\]|\[/quote\])}si', $string, NULL, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
    if ( sizeof( $chunks ) > 1 ) {
        $string = '';
        $quoteLevel = 0;
        $hadQuotes = false;
        foreach ( $chunks as $chunk ) {
            if ( preg_match( '{^\[quote}i', $chunk ) ) {
                $quoteLevel += 1;
                $string .= "\n";
                $hadQuotes = true;
            } elseif ( preg_match( '{^\[/quote}i', $chunk ) ) {
                if ( $quoteLevel > 0 ) {
                    $quoteLevel -= 1;
                }
                $string .= "\n";
                $hadQuotes = true;
            } else {
                if ( $quoteLevel == 0 ) {
                    $string .= $chunk;
                }
            }
        }
        // This part is optional, but it tends to improve the text.
        if ( $hadQuotes ) {
            // Quotes are typically found at the start of the message.
            $string = preg_replace( '/^\n+/', '', $string );
            // We inserted some newlines, so let's clean up any long runs.
            $string = preg_replace( '/\n\n\n+/', "\n\n", $string );
        }
    }
}
 
Top Bottom