1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Utils; |
12
|
|
|
|
13
|
|
|
use Transliterator; |
14
|
|
|
use Zemit\Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Slug |
18
|
|
|
* |
19
|
|
|
* @author Julien Turbide <[email protected]> |
20
|
|
|
* @copyright Zemit Team <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* @version 1.0 |
24
|
|
|
* |
25
|
|
|
* @package Zemit\Utils |
26
|
|
|
*/ |
27
|
|
|
class Slug |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Creates a slug to be used for pretty URLs. |
31
|
|
|
* |
32
|
|
|
* @param string $string |
33
|
|
|
* @param array $replace |
34
|
|
|
* @param string $delimiter |
35
|
|
|
* @return string |
36
|
|
|
* |
37
|
|
|
* @throws Exception |
38
|
|
|
*/ |
39
|
1 |
|
public static function generate($string, $replace = [], $delimiter = '-') |
40
|
|
|
{ |
41
|
1 |
|
if (!extension_loaded('intl')) { |
42
|
|
|
throw new Exception('intl module not loaded'); |
43
|
|
|
} |
44
|
1 |
|
if (!extension_loaded('mbstring')) { |
45
|
|
|
throw new Exception('mbstring module not loaded'); |
46
|
|
|
} |
47
|
|
|
// Save the old locale and set the new locale to UTF-8 |
48
|
1 |
|
$oldLocale = setlocale(LC_ALL, '0'); |
49
|
1 |
|
setlocale(LC_ALL, 'en_US.UTF-8'); |
50
|
|
|
// Better to replace given $replace array as index => value |
51
|
|
|
// Example $replace['ı' => 'i', 'İ' => 'i']; |
52
|
1 |
|
if (!empty($replace) && is_array($replace)) { |
53
|
|
|
$string = str_replace(array_keys($replace), array_values($replace), $string); |
54
|
|
|
} |
55
|
1 |
|
$transliterator = Transliterator::create('Any-Latin; Latin-ASCII'); |
56
|
1 |
|
$string = $transliterator->transliterate( |
57
|
1 |
|
mb_convert_encoding(htmlspecialchars_decode($string), 'UTF-8', 'auto') |
|
|
|
|
58
|
|
|
); |
59
|
1 |
|
self::restoreLocale($oldLocale); |
60
|
1 |
|
return self::cleanString($string, $delimiter); |
61
|
|
|
} |
62
|
|
|
/** |
63
|
|
|
* Revert back to the old locale |
64
|
|
|
*/ |
65
|
1 |
|
protected static function restoreLocale($oldLocale) |
66
|
|
|
{ |
67
|
1 |
|
if ((stripos($oldLocale, '=') > 0)) { |
68
|
1 |
|
parse_str(str_replace(';', '&', $oldLocale), $loc); |
69
|
1 |
|
$oldLocale = array_values($loc); |
70
|
|
|
} |
71
|
1 |
|
setlocale(LC_ALL, $oldLocale); |
72
|
|
|
} |
73
|
1 |
|
protected static function cleanString($string, $delimiter) |
74
|
|
|
{ |
75
|
|
|
// replace non letter or non digits by - |
76
|
1 |
|
$string = preg_replace('#[^\pL\d]+#u', '-', $string); |
77
|
|
|
// Trim trailing - |
78
|
1 |
|
$string = trim($string, '-'); |
79
|
1 |
|
$clean = preg_replace('~[^-\w]+~', '', $string); |
80
|
1 |
|
$clean = strtolower($clean); |
81
|
1 |
|
$clean = preg_replace('#[\/_|+ -]+#', $delimiter, $clean); |
82
|
1 |
|
$clean = trim($clean, $delimiter); |
83
|
1 |
|
return $clean; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|