An array of things that should replaced by the separator.
* @param bool $mergeKeep the previous (default) array-to-separator array.
* * @return void * * @psalm-param string[] $array */ public static function add_array_to_separator(array $array, bool $merge = true) { if ($merge === true) { self::$arrayToSeparator = \array_unique( \array_merge( self::$arrayToSeparator, $array ) ); } else { self::$arrayToSeparator = $array; } } /** * Add new characters to the list. `$map` should be a hash. * * @param array $map * @param string|null $language * * @return void * * @psalm-param arrayThe input string.
* @param string $languageYour primary language.
* @param string $unknownCharacter use if character unknown. (default is ?).
* * @return string */ public static function downcode( string $string, string $language = 'en', string $unknown = '' ): string { $string = self::expandString($string, $language); foreach (self::$maps as $mapsInner) { foreach ($mapsInner as $orig => $replace) { $string = \str_replace($orig, $replace, $string); } } $string = \voku\helper\ASCII::to_ascii( $string, $language, false, true ); return \voku\helper\ASCII::to_transliterate( $string, $unknown, false ); } /** * Convert a String to URL slug. Wraps filter() with a simpler * set of defaults for typical usage in generating blog post slugs. * * @param string $stringThe text you want to convert.
* @param int $maxLengthMax. length of the output string, set to "0" (zero) to * disable it
* @param string $separatorDefine a new separator for the words.
* @param string $languageThe language you want to convert to.
*/ public static function slug( string $string, int $maxLength = 200, string $separator = '-', string $language = 'en' ): string { return self::filter ($string, $maxLength, $language, false, false, true, $separator); } /** * Convert a String to URL. * * e.g.: "PettyThe text you want to convert.
* @param int $maxLengthMax. length of the output string, set to "0" (zero) to * disable it
* @param string $languageThe language you want to convert to.
* @param bool $fileName* Keep the "." from the extension e.g.: "imaäe.jpg" => * "image.jpg" *
* @param bool $removeWords
* Remove some "words" from the string.
* Info: Set extra words via remove_words().
*
Use strtolower() at the end.
* @param bool|string $separatorDefine a new separator for the words.
* * @return string */ public static function filter( string $string, int $maxLength = 200, string $language = 'en', bool $fileName = false, bool $removeWords = false, bool $strToLower = true, $separator = '-' ): string { if ($string === '') { return ''; } // fallback if ($language === '') { $language = 'en'; } // separator-fallback if ($separator === false) { $separator = '_'; } if ($separator === true || $separator === '') { $separator = '-'; } // escaped separator $separatorEscaped = \preg_quote($separator, '/'); // use defaults, if there are no values if (self::$arrayToSeparator === []) { self::reset_array_to_separator(); } // remove apostrophes which are not used as quotes around a string if (\strpos($string, "'") !== false) { $stringTmp = \preg_replace("/(\w)'(\w)/u", '${1}${2}', $string); if ($stringTmp !== null) { $string = (string) $stringTmp; } } // replace with $separator $string = (string) \preg_replace( self::$arrayToSeparator, $separator, $string ); // remove all other html-tags if ( \strpos($string, '<') !== false || \strpos($string, '>') !== false ) { $string = \strip_tags($string); } // use special language replacer $string = self::downcode($string, $language); // replace with $separator, again $string = (string) \preg_replace( self::$arrayToSeparator, $separator, $string ); // remove all these words from the string before urlifying $removeWordsSearch = '//'; if ($removeWords === true) { $removeList = self::get_remove_list($language); if ($removeList !== []) { $removeWordsSearch = '/\b(?:' . \implode('|', $removeList) . ')\b/ui'; } } // keep the "." from e.g.: a file-extension? if ($fileName) { $removePatternAddOn = '.'; } else { $removePatternAddOn = ''; } $string = (string) \preg_replace( [ // 1) remove un-needed chars '/[^' . $separatorEscaped . $removePatternAddOn . '\-a-zA-Z0-9\s]/u', // 2) convert spaces to $separator '/[\s]+/u', // 3) remove some extras words $removeWordsSearch, // 4) remove double $separator's '/[' . ($separatorEscaped ?: ' ') . ']+/u', // 5) remove $separator at the end '/[' . ($separatorEscaped ?: ' ') . ']+$/u', ], [ '', $separator, '', $separator, '', ], $string ); // "substr" only if "$length" is set if ( $maxLength && $maxLength > 0 && \strlen($string) > $maxLength ) { $string = (string) \substr(\trim($string, $separator), 0, $maxLength); } // convert to lowercase if ($strToLower === true) { $string = \strtolower($string); } // trim "$separator" from beginning and end of the string return \trim($string, $separator); } /** * Append words to the remove list. Accepts either single words or an array of words. * * @param string|string[] $words * @param string $language * @param bool $mergeKeep the previous (default) remove-words array.
* * @return void */ public static function remove_words($words, string $language = 'en', bool $merge = true) { if (\is_array($words) === false) { $words = [$words]; } foreach ($words as $removeWordKey => $removeWord) { $words[$removeWordKey] = \preg_quote($removeWord, '/'); } if ($merge === true) { self::$remove_list[$language] = \array_unique( \array_merge( self::get_remove_list($language), $words ) ); } else { self::$remove_list[$language] = $words; } } /** * Reset the internal "self::$arrayToSeparator" to the default values. * * @return void */ public static function reset_array_to_separator() { self::$arrayToSeparator = [ '/"|&|<|>|–|—/i', // ", &, <, >, –, — '/⁻|-|—|_|"|`|´|\'/', "#/\r\n|\r|\n|