web/lib/Formatter.php
2019-01-18 13:40:02 -06:00

30 lines
824 B
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?
class Formatter{
public static function MakeUrlSafe(string $text): string{
// Remove apostrophes
// We have to do this first so iconv doesn't choke
$text = str_replace(['\'', ',', ''], '', $text);
// Remove accent characters
$text = iconv('UTF-8', 'ASCII//TRANSLIT', $text) ?: '';
// Trim and convert to lowercase
$text = mb_strtolower(trim($text));
// Then convert any non-digit, non-letter character to a space
$text = preg_replace('/[^0-9a-zA-Z]/ius', ' ', $text) ?: '';
// Then convert any instance of one or more space to dash
$text = preg_replace('/\s+/ius', '-', $text) ?: '';
// Finally, trim dashes
$text = trim($text, '-');
return $text;
}
public static function ToPlainText(string $text): string{
return htmlspecialchars(trim($text), ENT_QUOTES, 'UTF-8');
}
}
?>