Zen Cart Logo
Forums / Bug Reports / [Not a Bug] Bug in zen_truncate_paragraph function

[Not a Bug] Bug in zen_truncate_paragraph function

Locked

Views: 2,013

Results 1 to 4 of 4
This thread is locked. New replies are disabled.
20 Dec 2007, 9:04 PM
#1
silverzulu avatar

silverzulu

New Zenner

Join Date:
Nov 2007
Posts:
10
Plugin Contributions:
0

[Not a Bug] Bug in zen_truncate_paragraph function

I'm using V1.3.7.1 and notice a bug in the zen_truncate_paragraph() function. This can be found in includes\functions\functions_general.php

The symptom of this bug is that meta tag descriptions are not shorten. For example;
MAX_META_TAG_DESCRIPTION_LENGTH=120 and the string to be shorten equals 250 characters. This function will return the total string (250 characters) and not the shorten string. The reason being is that the function compares the number of words to the numbers of characters which creates this bug. Near line 1329 and 1330, you find this;

    if ($zv_total > $size) {
      for ($x=0; $x < $size; $x++) {

$sv_total is the number of words and $size is the maximum number of characters. One is comparing apples with oranges, so to say.

Fix
Replace this function with the following code;

  function zen_truncate_paragraph($paragraph, $size = 100) {
    $zv_paragraph = trim($paragraph);

    // if it's less than the size given, then return it
    if (strlen($zv_paragraph) <= $size) return $zv_paragraph;

    // backtrack to find the end of the last complete word
	$len = $size;
	while ($zv_paragraph[$len] != ' ' && $len) --$len;
    return ($len)? substr($zv_paragraph, 0, $len) : substr($zv_paragraph, 0, $size);
}
20 Dec 2007, 9:51 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: [Not a Bug] Bug in zen_truncate_paragraph function

I gather you were interpreting that the zen_truncate_paragraph() function was supposed to truncate to a certain number of characters?

It's actually intended to truncate at a certain number of words.
... in which case, it's correct and there's no bug.

Note the description for MAX_META_TAG_DESCRIPTION_LENGTH:> Meta Tags Generated Description Maximum Length?

Set Generated Meta Tag Description Maximum Length to (words)
Default: 50

20 Dec 2007, 11:52 PM
#3
silverzulu avatar

silverzulu

New Zenner

Join Date:
Nov 2007
Posts:
10
Plugin Contributions:
0

Re: [Not a Bug] Bug in zen_truncate_paragraph function

:oops:, did not notice the description for MAX_META_TAG_DESCRIPTION_LENGTH. Thanks for that DrByte. I kind of assumed from the define label that the word DESCRIPTION_LENGTH would normally be the number of characters. A better define might be MAX_META_TAG_NUMBER_WORDS, but as a programmer I know how it goes.

Functionally I believe that it would be better to have the maximum number characters (and where the last word is a complete word and not truncated) since it seems as though search engines are interested in maximum number of characters for the description (even though it seems difficult to tell which search engines use this or not). I have a multi-language site and 50 words in one language could be 30% longer or more in another language.

21 Dec 2007, 12:11 AM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: [Not a Bug] Bug in zen_truncate_paragraph function

If you want to limit to characters, then change the module to use zen_truncate_string instead of zen_truncate_paragraph