Looking at the toolkit API I found the str::slug function.
I tried to read this topic to get a sense of how to use the function, but it seems overkill for what I need.
I just need to split and slug a foreach containing tags. How could I do that?
PS. maybe only useful for noobs, but if in the API page there would be an example for each method, it would make learning kirby andPHP much easier I think!
/**
* Convert a string to a safe version to be used in a URL
*
* @param string $string The unsafe string
* @param string $separator To be used instead of space and other non-word characters.
* @return string The safe string
*/
public static function slug($string, $separator = null, $allowed = null) {
$separator = $separator ?: static::$defaults['slug']['separator'];
$allowed = $allowed ?: static::$defaults['slug']['allowed'];
$string = trim($string);
$string = static::lower($string);
$string = static::ascii($string);
// replace spaces with simple dashes
$string = preg_replace('![^' . $allowed . ']!i', $separator, $string);
// remove double dashes
$string = preg_replace('![' . preg_quote($separator) . ']{2,}!', $separator, $string);
// trim trailing and leading dashes
$string = trim($string, $separator);
// replace slashes with dashes
$string = str_replace('/', $separator, $string);
return $string;
}
(/kirby/toolkit/lib/str.php)
I hope the comments are self-explanatory, if not, please don’t hesitate to ask.
An example:
<?= str::slug(' Das ist aber eine Überraschung!!!') ?>
Will result in:
das-ist-aber-eine-ueberraschung
But you are right, some documentation would be great.
Not sure if that’s what you are after, but to loop through list of comma separated tags, you can do this: