]> _ Git - cubist_util.git/commitdiff
wip #3480 @0:10
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 6 Mar 2020 17:21:50 +0000 (18:21 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 6 Mar 2020 17:21:50 +0000 (18:21 +0100)
src/Cubist/Util/Str.php [new file with mode: 0644]

diff --git a/src/Cubist/Util/Str.php b/src/Cubist/Util/Str.php
new file mode 100644 (file)
index 0000000..53ef969
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+namespace Cubist\Util;
+
+class Str extends \Illuminate\Support\Str
+{
+    /**
+     * Generate a URL friendly "slug" from a given string.
+     *
+     * @param string $title
+     * @param string $separator
+     * @param string|null $language
+     * @return string
+     */
+    public static function slugCase($title, $separator = '-', $language = 'en')
+    {
+        $title = $language ? static::ascii($title, $language) : $title;
+
+        // Convert all dashes/underscores into separator
+        $flip = $separator === '-' ? '_' : '-';
+
+        $title = preg_replace('![' . preg_quote($flip) . ']+!u', $separator, $title);
+
+        // Replace @ with the word 'at'
+        $title = str_replace('@', $separator . 'at' . $separator, $title);
+
+        // Remove all characters that are not the separator, letters, numbers, or whitespace.
+        $title = preg_replace('![^' . preg_quote($separator) . '\pL\pN\s]+!u', '', $title);
+
+        // Replace all separator characters and whitespace by a single separator
+        $title = preg_replace('![' . preg_quote($separator) . '\s]+!u', $separator, $title);
+
+        return trim($title, $separator);
+    }
+
+
+}
\ No newline at end of file