1
|
|
|
<?php
|
2
|
|
|
namespace Ballen\Sluginator;
|
3
|
|
|
|
4
|
|
|
/**
|
5
|
|
|
* Sluginator
|
6
|
|
|
* A simple URL slug creation library for PHP, simply feed it a string (such as
|
7
|
|
|
* a blog title) and it will create a URL friendly slug.
|
8
|
|
|
*
|
9
|
|
|
* @author Bobby Allen <[email protected]>
|
10
|
|
|
* @license http://opensource.org/licenses/MIT
|
11
|
|
|
* @link https://github.com/allebb/sluginator
|
12
|
|
|
* @link http://www.bobbyallen.me
|
13
|
|
|
*/
|
14
|
|
|
class Slug
|
15
|
|
|
{
|
16
|
|
|
|
17
|
|
|
/**
|
18
|
|
|
* Stores the plaintext string.
|
19
|
|
|
*
|
20
|
|
|
* @var string
|
21
|
|
|
*/
|
22
|
|
|
protected $original;
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* Array that stores values that you wish to remove (characters to delete from the string)
|
26
|
|
|
*
|
27
|
|
|
* @var array
|
28
|
|
|
*/
|
29
|
|
|
protected $remove_values = array(
|
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
|
|
|
* Array of characters that should be converted to the 'space' character.
|
73
|
|
|
*
|
74
|
|
|
* @var array
|
75
|
|
|
*/
|
76
|
|
|
protected $space_values = [
|
77
|
|
|
' ',
|
78
|
|
|
];
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* The character to use for space replacement.
|
82
|
|
|
*
|
83
|
|
|
* @var string
|
84
|
|
|
*/
|
85
|
|
|
protected $space_char = '-';
|
86
|
|
|
|
87
|
|
|
/**
|
88
|
|
|
* URL encode the output?
|
89
|
|
|
*
|
90
|
|
|
* @var boolean
|
91
|
|
|
*/
|
92
|
|
|
protected $url_encode = false;
|
93
|
|
|
|
94
|
|
|
/**
|
95
|
|
|
* Convert the text to all lowercase characters?
|
96
|
|
|
*
|
97
|
|
|
* @var boolean
|
98
|
|
|
*/
|
99
|
|
|
protected $use_lowercase = false;
|
100
|
|
|
|
101
|
|
|
/**
|
102
|
|
|
* Stores the generated slug.
|
103
|
|
|
*
|
104
|
|
|
* @var string
|
105
|
|
|
*/
|
106
|
|
|
protected $slug;
|
107
|
|
|
|
108
|
|
|
/**
|
109
|
|
|
* Slug constructor.
|
110
|
|
|
*
|
111
|
|
|
* @param string $input The title or sentence to convert to a URL slug.
|
112
|
|
|
*/
|
113
|
9 |
|
public function __construct($input)
|
114
|
|
|
{
|
115
|
9 |
|
$this->original = $input;
|
116
|
9 |
|
$this->build();
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
/**
|
120
|
|
|
* Generate the slug.
|
121
|
|
|
*
|
122
|
|
|
* @return Slug
|
123
|
|
|
*/
|
124
|
9 |
|
public function build()
|
125
|
|
|
{
|
126
|
9 |
|
$slug = $this->original;
|
127
|
9 |
|
$slug = trim(strip_tags($slug));
|
128
|
9 |
|
$slug = str_replace($this->space_values, $this->space_char, $slug);
|
129
|
9 |
|
$slug = str_replace($this->remove_values, '', $slug);
|
130
|
|
|
|
131
|
9 |
|
if ($this->url_encode) {
|
132
|
1 |
|
$slug = urlencode($slug);
|
133
|
|
|
}
|
134
|
|
|
|
135
|
9 |
|
if ($this->use_lowercase) {
|
136
|
1 |
|
$slug = strtolower($slug);
|
137
|
|
|
}
|
138
|
|
|
|
139
|
9 |
|
$this->slug = $slug;
|
140
|
9 |
|
return $this;
|
141
|
|
|
}
|
142
|
|
|
|
143
|
|
|
/**
|
144
|
|
|
* Returns the generated slug string.
|
145
|
|
|
*
|
146
|
|
|
* @return string
|
147
|
|
|
*/
|
148
|
9 |
|
public function getSlug()
|
149
|
|
|
{
|
150
|
9 |
|
return $this->slug;
|
151
|
|
|
}
|
152
|
|
|
|
153
|
|
|
/**
|
154
|
|
|
* Returns the generated slug string
|
155
|
|
|
*
|
156
|
|
|
* @return string
|
157
|
|
|
*/
|
158
|
7 |
|
public function __toString()
|
159
|
|
|
{
|
160
|
7 |
|
return $this->getSlug();
|
161
|
|
|
}
|
162
|
|
|
|
163
|
|
|
/**
|
164
|
|
|
* Sets a new set of rules of which should be handled as 'space' characters.
|
165
|
|
|
*
|
166
|
|
|
* @param array $values
|
167
|
|
|
* @return Slug
|
168
|
|
|
*/
|
169
|
1 |
|
public function setSpaceValues(array $values)
|
170
|
|
|
{
|
171
|
1 |
|
$this->space_values = $values;
|
172
|
1 |
|
return $this;
|
173
|
|
|
}
|
174
|
|
|
|
175
|
|
|
/**
|
176
|
|
|
* Sets a new set of rules of which will be 'removed' from the strnig.
|
177
|
|
|
*
|
178
|
|
|
* @param array $values
|
179
|
|
|
* @return Slug
|
180
|
|
|
*/
|
181
|
1 |
|
public function setRemoveValues(array $values)
|
182
|
|
|
{
|
183
|
1 |
|
$this->remove_values = $values;
|
184
|
1 |
|
return $this;
|
185
|
|
|
}
|
186
|
|
|
|
187
|
|
|
/**
|
188
|
|
|
* Add a new item to the Space values ruleset.
|
189
|
|
|
*
|
190
|
|
|
* @param string $value
|
191
|
|
|
* @return Slug
|
192
|
|
|
*/
|
193
|
1 |
|
public function addSpaceValue($value)
|
194
|
|
|
{
|
195
|
1 |
|
$this->space_values[] = $value;
|
196
|
1 |
|
return $this;
|
197
|
|
|
}
|
198
|
|
|
|
199
|
|
|
/**
|
200
|
|
|
* Add a new item to the remove values ruleset.
|
201
|
|
|
*
|
202
|
|
|
* @param string $value
|
203
|
|
|
* @return Slug
|
204
|
|
|
*/
|
205
|
1 |
|
public function addRemoveValue($value)
|
206
|
|
|
{
|
207
|
1 |
|
$this->remove_values[] = $value;
|
208
|
1 |
|
return $this;
|
209
|
|
|
}
|
210
|
|
|
|
211
|
|
|
/**
|
212
|
|
|
* Convert generated slugs to lowercase strings.
|
213
|
|
|
*
|
214
|
|
|
* @param boolean $lowercase
|
215
|
|
|
* @return Slug
|
216
|
|
|
*/
|
217
|
1 |
|
public function setUseLowercase($lowercase = true)
|
218
|
|
|
{
|
219
|
1 |
|
$this->use_lowercase = $lowercase;
|
220
|
1 |
|
return $this;
|
221
|
|
|
}
|
222
|
|
|
|
223
|
|
|
/**
|
224
|
|
|
* Convert generated slugs to lowercase strings.
|
225
|
|
|
*
|
226
|
|
|
* @param boolean $encode
|
227
|
|
|
* @return Slug
|
228
|
|
|
*/
|
229
|
1 |
|
public function setUrlEncode($encode = true)
|
230
|
|
|
{
|
231
|
1 |
|
$this->url_encode = $encode;
|
232
|
1 |
|
return $this;
|
233
|
|
|
}
|
234
|
|
|
|
235
|
|
|
/**
|
236
|
|
|
* Sets the character of which is used to replace spaces.
|
237
|
|
|
*
|
238
|
|
|
* @param string $char
|
239
|
|
|
* @return Slug
|
240
|
|
|
*/
|
241
|
1 |
|
public function setSpaceCharacter($char = '-')
|
242
|
|
|
{
|
243
|
1 |
|
$this->space_char = $char;
|
244
|
1 |
|
return $this;
|
245
|
|
|
}
|
246
|
|
|
}
|
247
|
|
|
|