Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
Hello, i'm having issues with the excerpt tag. It seems that excerpt sometimes adds one extra word after the end of a line when outputting content.

For instance; I would like to excerpt the following text at count 23, so that it returns only the first two sentences:

---

Mobile phones en apps zijn hypersensitief. Na distributie van een mobile phone en app is het niet meer mogelijk een aanpassing te doen.

Uw klant verwacht het beste van u. De minste hapering kan ertoe leiden dat geswitcht wordt van app of mobile phone. De kans op imagoschade ligt altijd op de loer. Met het specialistische testwerk van Specialisterren voorkomt u dat.

---

I'm using:
Code: Select all
<p><cms:excerpt count='23'><cms:show content /></cms:excerpt></p>

However: Couch returns the first two sentences, plus the first word of the next sentence ("Uw..."), so 24 words in total. When I set count to 22, it shows exactly 22 words, when count is set to 24, it shows 25 words.

The source of the sentences don't show any extra/strange html.

Also, just to be sure I replaced '<cms:show content />' with plain text, in which case count also works as expected.

Am I missing something here?

Attachments

Hi Vincent,

The current logic of cms:excerpt tag, when truncating whole words (as opposed to characters), simply 'explodes' the string at space character. What seems to be happening in your case is that there is no space character between 'doen.' and 'Uw' (there is a 'newline instead)
te doen.

Uw

So, 'doen. Uw' is being considered a single word. That explains the wrong count.

I think we need to expand the logic used by this tag while truncating single characters (converts newlines to spaces, coalesces multiple spaces into one etc.) to truncating whole words as well.

Please edit your couch/tags.php file, find 'function excerpt' (Line 2795 to 2847) and replace it with the revised function given below -
Code: Select all
    function excerpt($params, $node ){
            global $CTX, $FUNCS;

            extract( $FUNCS->get_named_vars(
                        array( 'count'=>'',
                              'allow'=>'',
                              'trail'=>'&hellip;',
                              'truncate_chars'=>'0'
                             ),
                        $params) );

            $count = $FUNCS->is_non_zero_natural( $count ) ? intval( $count ) : 50;

            $allowed_tags = '';
            $truncate_chars = ( intval($truncate_chars)==1 ) ? 1 : 0;
            if( !$truncate_chars ){ // $allowed_tags have to be ignored for chars
                if( $allow!='' ){
                    $allow = explode( ",", $allow );
                    $allow = array_map( "trim", $allow );
                }
                if( is_array($allow) ){
                    foreach( $allow as $tag ){
                        if( $tag!='') $allowed_tags .= '<'.$tag.'>';
                    }
                }
            }

            // call the children
            foreach( $node->children as $child ){
                $html .= $child->get_HTML();
            }

            $str_utf = strip_tags( $html, $allowed_tags );

            // Replace all '&nbsp;' occurances with spaces
            $str_utf = str_replace( "&nbsp;", " ", $str_utf );

            // Coalese multiple 'white-space characters' (space, tabs, linebreaks etc.) into single spaces
            $str_utf = preg_replace( "/\s+/m", " ", $str_utf );

            // Trim off leading and trailing spaces
            $str_utf = trim( $str_utf );

            if( $truncate_chars ){
                $html = $FUNCS->excerpt( $str_utf, $count, $trail );
            }
            else{ // truncate whole words (default)
                $arr = explode( ' ', $str_utf, $count+1 );
                if( count($arr) > $count ){
                    $sep = $trail;
                    $arr = array_slice( $arr, 0, -1 );
                }
                $html = implode( ' ', $arr );
            }
           
            return $html . $sep;
        }

Please let me know if that makes cms:excerpt behave as you expect.

Thanks.
Yes!

Many thanks for the explanation and working solution.
Thanks for letting me know.
I'll now commit the modification into the core.
4 posts Page 1 of 1