nystudio107 / seomatic

DEPRECATED A turnkey SEO implementation for Craft CMS 2.x that is comprehensive, powerful, and flexible
Other
357 stars 45 forks source link

Suggestion: Truncate description on last word #227

Closed tomkiss closed 7 years ago

tomkiss commented 7 years ago

In the process of integrating SEOmatic in to a site I'm working on.

Our existing SEO solution has a function to truncate text, similar to SEOmatic. One thing that is nice though, is to truncate on a sentence (ending in . or !), if one appears in < 170 characters.

Something like:

  public function truncateDescription($desc) {
    // Use truncated entry text:
    // strip html tags, strip new lines, substr to 170 chars
    $desc = trim(preg_replace('/\s+/', ' ', strip_tags($desc)));
    $desc = substr($desc, 0, 170);
    // If period or exclamation found, substr to that, ignore short strings
    $posP = strrpos($desc, '.');
    $posE = strrpos($desc, '!');
    $pos = ($posP > $posE ? $posP : $posE);
    if(!empty($pos) && $pos > 30) {
      $desc = substr($desc, 0, $pos+1);
    }
    return $desc;
  }

This may not be desirable for everyone but it's worked well for us.

Results in this:

Darth Vader, also known by his birth name Anakin Skywalker, is a fictional character in the Star Wars franchise.

Rather than this:

Darth Vader, also known by his birth name Anakin Skywalker, is a fictional character in the Star Wars franchise. Vader appears in the original film trilogy as a pivotal

khalwat commented 7 years ago

So the important thing to keep in mind is that it's a bot, not a person that is parsing the SEO description. As such, it's less important that sentence structure is intact, and more important that the keywords are there.

What it really boils down to, though, is that people who create the SEO description craft it specifically for the SEO description field, and naturally have the sentences end properly.

That's not always the case, so then we fall back on automated truncating of the SEO description, and as per the above, I think it's probably better to do it on the word level rather than the sentence level.

ellierider commented 1 year ago

Just to add some color here. When those descriptions are then meant to be translated by an external service, it does poorly when the full sentence is not available.

khalwat commented 1 year ago

@ellierider that's a good use-case. You can increase the length that SEOmatic will truncate at to whatever you like in the Plugin settings