php - Shorten a string to allowed length

I'm writing a functionality that take a string and converts it into a URL slug. It works fine, but in some cases I end-up with slugs exceeding my limit of 65 chars, like this one:

emiteria-matie-martinez-robinson-viles-1888-1961-the-viles-foundation-and-matie-viles

So I need to add another check for length and if it exceeds, cut it down based on a few filter rules to make it no longer than my limit. I don't want to filter all the words, just take out one at a time until it's below 66 chars. And if the str is still too long after trying to remove terms from the filter, start removing the last word, until the length criteria is satisfied.

I started writing this but it seems a bit inefficient. Am I on the right track?

$limit = 65;

$filter = array("and","the","of","at","de","en","la","on","s");

$str = 'emiteria-matie-martinez-robinson-viles-1888-1961-the-viles-foundation-and-matie-viles';

if (strlen($str) > $limit) {
    removePart($str);

}  

function removePart($str) {
   $arr = explode("-",$str);
   // remove one part from filter
   $str = implode(" ",$arr);
}

Answer

Solution:

<?php

$limit = 65;

$filter = array("and","the","of","at","de","en","la","on","s");

$str = 'emiteria-matie-martinez-robinson-viles-1888-1961-the-viles-foundation-and-matie-viles';

while (strlen($str) > $limit) {
    $str = removePart($str);

}

echo $str;

function removePart($str) {
   $arr = explode('-',$str);
   array_pop($arr); // remove last element
   $str = implode('-',$arr);
   
   return $str;
}

Answer

Solution:

First you need to remove first word from the filter array while your string is longer than the limit. removePart function removes single (first) word that matches one from the filters. To remove more - call it multiple times in a while loop. After this remove words from the end - one by one - until the string is short enough.

<?php

$limit = 65;

$filter = array("and","the","of","at","de","en","la","on","s");

$str = 'emiteria-matie-martinez-robinson-viles-1888-1961-the-viles-foundation-and-matie-viles';

while (strlen($str) > $limit) {
    $strlen = strlen($str);
    $str = removePart($str, $filter);

    if (strlen($str) === $strlen) {
        break;
    }
    var_dump($str);
}  

while (strlen($str) > $limit) {
  $arr = explode("-",$str);
  array_pop($arr);
  $str = implode("-",$arr);

  var_dump($str);
}


function removePart($str, $filter) {
   $arr = explode("-",$str);
   // remove one part from filter
   foreach ($arr as $i => $word) {
     if (in_array($word, $filter)) {
       unset($arr[$i]);
       break;
     }
   }
   $str = implode("-",$arr);

   return $str;
}

Output:

string(81) "emiteria-matie-martinez-robinson-viles-1888-1961-viles-foundation-and-matie-viles" // removed the
string(77) "emiteria-matie-martinez-robinson-viles-1888-1961-viles-foundation-matie-viles" // removed and
string(71) "emiteria-matie-martinez-robinson-viles-1888-1961-viles-foundation-matie" // removed last word
string(65) "emiteria-matie-martinez-robinson-viles-1888-1961-viles-foundation" // removed last word

Source