forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slug.php
81 lines (71 loc) · 3.09 KB
/
Slug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
| |
| The code below is inspired by Matteo Spinelli's (cubiq.org) blog post |
| http://cubiq.org/the-perfect-php-clean-url-generator |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Nikolaos Dimopoulos <[email protected]> |
| Ilgıt Yıldırım <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Utils;
use Phalcon\Exception;
/**
* Slug component
*
* @package Phalcon\Utils
*/
class Slug
{
/**
* Creates a slug to be used for pretty URLs.
*
* @param string $string
* @param array $replace
* @param string $delimiter
* @return string
*
* @throws \Phalcon\Exception
*/
public static function generate($string, $replace = [], $delimiter = '-')
{
if (!extension_loaded('intl')) {
throw new Exception('intl module not loaded');
}
// Save the old locale and set the new locale to UTF-8
$oldLocale = setlocale(LC_ALL, '0');
setlocale(LC_ALL, 'en_US.UTF-8');
// Better to replace given $replace array as index => value
// Example $replace['ı' => 'i', 'İ' => 'i'];
if (!empty($replace) && is_array($replace)) {
$string = str_replace(array_keys($replace), array_values($replace), $string);
}
$transliterator = \Transliterator::create('Any-Latin; Latin-ASCII');
$string = $transliterator->transliterate(
mb_convert_encoding(htmlspecialchars_decode($string), 'UTF-8', 'auto')
);
// replace non letter or non digits by -
$string = preg_replace('#[^\pL\d]+#u', '-', $string);
// Trim trailing -
$string = trim($string, '-');
$clean = preg_replace('~[^-\w]+~', '', $string);
$clean = strtolower($clean);
$clean = preg_replace('#[\/_|+ -]+#', $delimiter, $clean);
$clean = trim($clean, $delimiter);
// Revert back to the old locale
setlocale(LC_ALL, $oldLocale);
return $clean;
}
}