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);
}
<?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;
}
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
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Foundation, similar to Bootstrap, has become very popular as a more complex framework with some advanced but easy-to-implement CSS components. It is built with Sass, so just like Bootstrap, it is customizable. In addition to this, it also boasts some features that help make the design mobile responsive.
https://get.foundation/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.