1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jaybizzle\Safeurl; |
4
|
|
|
|
5
|
|
|
class Safeurl |
6
|
|
|
{ |
7
|
|
|
public $decode; // Decode html entities in string? |
8
|
|
|
public $decode_charset; // Charset to use if $decode is set to true |
9
|
|
|
public $lowercase; // Turns string into all lowercase letters |
10
|
|
|
public $strip; // Strip out html tags from string? |
11
|
|
|
public $maxlength; // Maximum length of resulting title |
12
|
|
|
public $whole_word; // If maxlength is reached, chop at nearest whole word? or hard chop? |
13
|
|
|
public $blank; // What title to use if no alphanumeric characters can be found |
14
|
|
|
public $separator; // Allow a differnt character to be used as the separator. |
15
|
|
|
public $translation_table; // A table of UTF-8 characters and what to make them. |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class constructor. |
19
|
|
|
*/ |
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
// setup the default options |
24
|
|
|
$default = config('safeurl'); |
25
|
|
|
|
26
|
|
|
foreach ($default as $property => $value) { |
27
|
|
|
$this->$property = $value; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* the worker method. |
33
|
|
|
* |
34
|
|
|
* @param string $text |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function make($text, $options = null) |
39
|
|
|
{ |
40
|
|
|
$this->setUserOptions($options); // set user defined options |
41
|
|
|
|
42
|
|
|
$text = $this->decode($text); // decode UTF-8 chars |
43
|
|
|
|
44
|
|
|
$text = trim($text); // trim |
45
|
|
|
|
46
|
|
|
$text = $this->lower($text); // convert to lowercase |
47
|
|
|
|
48
|
|
|
$text = $this->strip($text); // strip HTML |
49
|
|
|
|
50
|
|
|
$text = $this->filter($text); // filter the input |
51
|
|
|
|
52
|
|
|
//chop? |
53
|
|
|
if (strlen($text) > $this->maxlength) { |
54
|
|
|
$text = substr($text, 0, $this->maxlength); |
55
|
|
|
|
56
|
|
|
if ($this->whole_word) { |
57
|
|
|
/* |
58
|
|
|
* If maxlength is small and leaves us with only part of one |
59
|
|
|
* word ignore the "whole_word" filtering. |
60
|
|
|
*/ |
61
|
|
|
$words = explode($this->separator, $text); |
62
|
|
|
$temp = implode($this->separator, array_diff($words, [array_pop($words)])); |
63
|
|
|
if ($temp != '') { |
64
|
|
|
$text = $temp; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$text = rtrim($text, $this->separator); // remove any trailing separators |
69
|
|
|
} |
70
|
|
|
//return =] |
71
|
|
|
if ($text == '') { |
72
|
|
|
return $this->blank; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $text; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set user defined options. |
80
|
|
|
* |
81
|
|
|
* @param array $options |
82
|
|
|
*/ |
83
|
|
|
private function setUserOptions($options) |
84
|
|
|
{ |
85
|
|
|
if (is_array($options)) { |
86
|
|
|
foreach ($options as $property => $value) { |
87
|
|
|
$this->$property = $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Helper method that uses the translation table to convert |
94
|
|
|
* non-ascii characters to a resonalbe alternative. |
95
|
|
|
* |
96
|
|
|
* @param string $text |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function convertCharacters($text) |
101
|
|
|
{ |
102
|
|
|
$text = html_entity_decode($text, ENT_QUOTES, $this->decode_charset); |
103
|
|
|
$text = strtr($text, $this->translation_table); |
104
|
|
|
|
105
|
|
|
return $text; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Decode HTML entities and UTF-8 characters. |
110
|
|
|
* |
111
|
|
|
* @param string $text |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
private function decode($text) |
116
|
|
|
{ |
117
|
|
|
return ($this->decode) ? $this->convertCharacters($text) : $text; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Convert string to lowercase. |
122
|
|
|
* |
123
|
|
|
* @param string $text |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
private function lower($text) |
128
|
|
|
{ |
129
|
|
|
return ($this->lowercase) ? strtolower($text) : $text; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Strip HTML tages. |
134
|
|
|
* |
135
|
|
|
* @param string $text |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
private function strip($text) |
140
|
|
|
{ |
141
|
|
|
return ($this->strip) ? strip_tags($text) : $text; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Strip anything that isn't alphanumeric or an underscore. |
146
|
|
|
* |
147
|
|
|
* @param string $text |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
private function filter($text) |
152
|
|
|
{ |
153
|
|
|
$text = preg_replace("/[^&a-z0-9-_\s']/i", '', $text); |
154
|
|
|
$text = str_replace(' ', $this->separator, $text); |
155
|
|
|
$text = trim(preg_replace("/{$this->separator}{2,}/", $this->separator, $text), $this->separator); |
156
|
|
|
|
157
|
|
|
return $text; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/* |
162
|
|
|
* Thanks to timemachine3030 for the idea/implementation for this class. |
163
|
|
|
* https://github.com/timemachine3030 |
164
|
|
|
*/ |
165
|
|
|
|