Auto Generated Meta Description Length

I have been getting notices from search engines that my meta description length was too long. Upon investigation, I found that my meta descriptions were anywhere from 250-300 characters in length. Most suggest that the length should be 155 or less.

CSC has $max_letters = 250 which is obviously too much and I am not sure why some of my meta descriptions were more than 250. After some testing, I found that $max_letters = 130 puts me just under 155 while $max_letters = 140 puts me just over 155. I stuck with 130 to be on the safe side.

/app/functions/fn.common.php

/**
 * Return cleaned text string (for meta description use)
 *
 * @param string $html - html code to generate description from
 * @param int $max_letters - maximum letters in description
 * @return string - cleaned text
 */
function fn_generate_meta_description($html, $max_letters = 130)
{
    $meta = array();
    if (!empty($html)) {
        $html = str_replace(' ', ' ', $html);
        $html = str_replace(array("\r\n", "\n", "\r"), ' ', html_entity_decode(trim($html), ENT_QUOTES, 'UTF-8'));
        $html = preg_replace('/\/i', " ", $html);
        $html = preg_replace('|]*>.*?|i', '', $html);
        $html = strip_tags($html);
        $html = str_replace(array('.', ',', ':', ';', '`', '"', '~', '\'', '(', ')'), ' ', $html);
        $html = preg_replace('/\s+/', ' ', $html);
        $html = explode(' ', $html);
        $letters_count = 0;
        foreach ($html as $k => $v) {
            $letters_count += fn_strlen($v);
            if ($letters_count <= $max_letters) {
                $meta[] = $v;
            } else {
                break;
            }
        }
    }
return implode(' ', $meta);

}

This function calculates $letters_count incorrectly. Instead of the following line:

$letters_count += fn_strlen($v);

there should be something like this (add 1 character for the space between words):

$letters_count += fn_strlen($v) + (empty($meta) ? 0 : 1);

because of this one (add spaces between words):

return implode(' ', $meta);

Thanks eCom. I didn't have time to look into finding out why there was a miscount but you are correct that it wasn't counting the spaces. Your solution gives me more of a precise count so that I can use $max_letters = 155 now.

Edit: I just wanted to let everyone else reading this that Google was not reporting my meta descriptions as being too long with the original code (not sure that Google really cares since I have seen various versions of my descriptions in Google's search) but Bing and some others were.

@TheTool

What does this code do for you that you posted exactly ? Does it limit the amount of characters you can add in the meta tags of products, categories and pages ?

@TheTool

What does this code do for you that you posted exactly ? Does it limit the amount of characters you can add in the meta tags of products, categories and pages ?

This is based on the auto generated meta description meaning what the system generates if you do not input a meta description yourself.

Can anyone tell me what the meta description length limit is now in v4.7, 4.5

Thanks

Can anyone tell me what the meta description length limit is now in v4.7, 4.5

Thanks

It hasn't changed. It is still 250.

Found it, changed in here to 320

https://prnt.sc/jricio

Oh, I thought you were referring to the auto generated meta description which is what the thread is discussing.

Found it, changed in here to 320

https://prnt.sc/jricio

Please also make the same changes in cscart_ult_product_descriptions table

Oh, I thought you were referring to the auto generated meta description which is what the thread is discussing.

My Mistake, I got the 2 confused

@ecomlabs, will do thanks