1
|
|
|
<?php /** @noinspection PhpInternalEntityUsedInspection */ |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* citeproc-php |
5
|
|
|
* |
6
|
|
|
* @link http://github.com/seboettg/citeproc-php for the source repository |
7
|
|
|
* @copyright Copyright (c) 2016 Sebastian Böttger. |
8
|
|
|
* @license https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Seboettg\CiteProc\Util; |
12
|
|
|
|
13
|
|
|
use Seboettg\CiteProc\CiteProc; |
14
|
|
|
use function mb_detect_encoding; |
15
|
|
|
use function mb_strlen; |
16
|
|
|
use function mb_strpos; |
17
|
|
|
use function mb_strtolower; |
18
|
|
|
use function mb_strtoupper; |
19
|
|
|
use function mb_substr; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class StringHelper |
23
|
|
|
* @package Seboettg\CiteProc\Util |
24
|
|
|
* |
25
|
|
|
* @author Sebastian Böttger <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class StringHelper |
28
|
|
|
{ |
29
|
|
|
const PREPOSITIONS = [ |
30
|
|
|
'on', 'in', 'at', 'since', 'for', 'ago', 'before', 'to', 'past', 'till', 'until', 'by', 'under', 'below', |
31
|
|
|
'over', 'above', 'across', 'through', 'into', 'towards', 'onto', 'from', 'of', 'off', 'about', 'via' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
const ARTICLES = [ |
35
|
|
|
'a', 'an', 'the' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
const ADVERBS = [ |
39
|
|
|
'yet', 'so', 'just', 'only' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
const CONJUNCTIONS = [ |
43
|
|
|
'nor', 'so', 'and', 'or' |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
const ADJECTIVES = [ |
47
|
|
|
'down', 'up' |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
const ISO_ENCODINGS = [ |
51
|
|
|
'ISO-8859-1', |
52
|
|
|
'ISO-8859-2', |
53
|
|
|
'ISO-8859-3', |
54
|
|
|
'ISO-8859-4', |
55
|
|
|
'ISO-8859-5', |
56
|
|
|
'ISO-8859-6', |
57
|
|
|
'ISO-8859-7', |
58
|
|
|
'ISO-8859-8', |
59
|
|
|
'ISO-8859-9', |
60
|
|
|
'ISO-8859-10', |
61
|
|
|
'ISO-8859-11', |
62
|
|
|
'ISO-8859-13', |
63
|
|
|
'ISO-8859-14', |
64
|
|
|
'ISO-8859-15', |
65
|
|
|
'ISO-8859-16' |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* opening quote sign |
70
|
|
|
*/ |
71
|
|
|
const OPENING_QUOTE = "“"; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* closing quote sign |
75
|
|
|
*/ |
76
|
|
|
const CLOSING_QUOTE = "”"; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $text |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public static function capitalizeAll($text) |
83
|
|
|
{ |
84
|
|
|
$wordArray = explode(" ", $text); |
85
|
|
|
|
86
|
|
|
array_walk($wordArray, function (&$word) { |
87
|
|
|
$word = ucfirst($word); |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
return implode(" ", $wordArray); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $titleString |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public static function capitalizeForTitle($titleString) |
98
|
|
|
{ |
99
|
|
|
if (preg_match('/(.+[^\<\>][\.:\/;\?\!]\s?)([a-z])(.+)/', $titleString, $match)) { |
100
|
|
|
$titleString = $match[1] . StringHelper::ucfirst($match[2]) . $match[3]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$wordArray = explode(" ", $titleString); |
104
|
|
|
|
105
|
|
|
array_walk($wordArray, function (&$word) { |
106
|
|
|
$words = explode("-", $word); |
107
|
|
|
if (count($words) > 1) { |
108
|
|
|
array_walk($words, function (&$w) { |
109
|
|
|
$w = StringHelper::keepLowerCase($w) ? $w : StringHelper::ucfirst($w); |
110
|
|
|
}); |
111
|
|
|
$word = implode("-", $words); |
112
|
|
|
} |
113
|
|
|
$word = StringHelper::keepLowerCase($word) ? $word : StringHelper::ucfirst($word); |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
return implode(" ", $wordArray); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $word |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public static function keepLowerCase($word) |
124
|
|
|
{ |
125
|
|
|
// keep lower case if the first char is not an utf-8 letter |
126
|
|
|
return in_array($word, self::PREPOSITIONS) || |
127
|
|
|
in_array($word, self::ARTICLES) || |
128
|
|
|
in_array($word, self::CONJUNCTIONS) || |
129
|
|
|
in_array($word, self::ADJECTIVES) || |
130
|
|
|
(bool) preg_match("/[^\p{L}].+/", $word); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $string |
135
|
|
|
* @param string $encoding |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public static function ucfirst($string, $encoding = 'UTF-8') |
139
|
|
|
{ |
140
|
|
|
$strlen = mb_strlen($string, $encoding); |
141
|
|
|
$firstChar = mb_substr($string, 0, 1, $encoding); |
142
|
|
|
$then = mb_substr($string, 1, $strlen - 1, $encoding); |
143
|
|
|
|
144
|
|
|
/** @noinspection PhpInternalEntityUsedInspection */ |
145
|
|
|
$encoding = mb_detect_encoding($firstChar, self::ISO_ENCODINGS, true); |
146
|
|
|
return in_array($encoding, self::ISO_ENCODINGS) ? |
147
|
|
|
mb_strtoupper($firstChar, $encoding) . $then : $firstChar.$then; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public static function strrev($string) |
151
|
|
|
{ |
152
|
|
|
$result = ''; |
153
|
|
|
for ($i = mb_strlen($string); $i >= 0; --$i) { |
154
|
|
|
$result .= mb_substr($string, $i, 1); |
155
|
|
|
} |
156
|
|
|
return $result; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $delimiter |
161
|
|
|
* @param string[] $arrayOfStrings |
162
|
|
|
* @return string; |
163
|
|
|
*/ |
164
|
|
|
public static function implodeAndPreventConsecutiveChars($delimiter, $arrayOfStrings) |
165
|
|
|
{ |
166
|
|
|
$delim = trim($delimiter); |
167
|
|
|
if (!empty($delim)) { |
168
|
|
|
foreach ($arrayOfStrings as $key => $textPart) { |
169
|
|
|
$pos = mb_strpos(StringHelper::strrev($textPart), StringHelper::strrev($delim)); |
170
|
|
|
if ($pos === 0) { |
171
|
|
|
$length = mb_strlen($textPart) - mb_strlen($delim); |
172
|
|
|
$textPart = mb_substr($textPart, 0, $length); |
173
|
|
|
$arrayOfStrings[$key] = $textPart; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
return implode($delimiter, $arrayOfStrings); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param $string |
182
|
|
|
* @param $initializeSign |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public static function initializeBySpaceOrHyphen($string, $initializeSign) |
186
|
|
|
{ |
187
|
|
|
$initializeWithHyphen = CiteProc::getContext()->getGlobalOptions()->isInitializeWithHyphen(); |
188
|
|
|
$res = ""; |
189
|
|
|
$exploded = explode("-", $string); |
190
|
|
|
$i = 0; |
191
|
|
|
foreach ($exploded as $explode) { |
192
|
|
|
$spaceExploded = explode(" ", $explode); |
193
|
|
|
foreach ($spaceExploded as $givenPart) { |
194
|
|
|
$firstLetter = mb_substr($givenPart, 0, 1, "UTF-8"); |
195
|
|
|
if (StringHelper::isLatinString($firstLetter)) { |
196
|
|
|
$res .= ctype_upper($firstLetter) ? $firstLetter.$initializeSign : " ".$givenPart." "; |
197
|
|
|
} else { |
198
|
|
|
$res .= $firstLetter.$initializeSign; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
if ($i < count($exploded) - 1 && $initializeWithHyphen) { |
202
|
|
|
$res = rtrim($res)."-"; |
203
|
|
|
} |
204
|
|
|
++$i; |
205
|
|
|
} |
206
|
|
|
return $res; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param $string |
211
|
|
|
* @return mixed|string |
212
|
|
|
*/ |
213
|
|
|
public static function camelCase2Hyphen($string) |
214
|
|
|
{ |
215
|
|
|
$hyphenated = preg_replace("/([A-Z])/", "-$1", $string); |
216
|
|
|
$hyphenated = mb_substr($hyphenated, 0, 1) === "-" ? mb_substr($hyphenated, 1) : $hyphenated; |
217
|
|
|
return mb_strtolower($hyphenated); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param $string |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
public static function checkLowerCaseString($string) |
225
|
|
|
{ |
226
|
|
|
return ($string === mb_strtolower($string)); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param $string |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
public static function checkUpperCaseString($string) |
234
|
|
|
{ |
235
|
|
|
return ($string === mb_strtoupper($string)); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param $string |
240
|
|
|
* @return mixed |
241
|
|
|
*/ |
242
|
|
|
public static function clearApostrophes($string) |
243
|
|
|
{ |
244
|
|
|
return preg_replace("/\'/", "’", $string); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* replaces outer quotes of $text by given inner quotes |
249
|
|
|
* |
250
|
|
|
* @param $text |
251
|
|
|
* @param $outerOpenQuote |
252
|
|
|
* @param $outerCloseQuote |
253
|
|
|
* @param $innerOpenQuote |
254
|
|
|
* @param $innerCloseQuote |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public static function replaceOuterQuotes( |
258
|
|
|
$text, |
259
|
|
|
$outerOpenQuote, |
260
|
|
|
$outerCloseQuote, |
261
|
|
|
$innerOpenQuote, |
262
|
|
|
$innerCloseQuote |
263
|
|
|
) { |
264
|
|
|
if (preg_match("/(.*)$outerOpenQuote(.+)$outerCloseQuote(.*)/u", $text, $match)) { |
265
|
|
|
return $match[1].$innerOpenQuote.$match[2].$innerCloseQuote.$match[3]; |
266
|
|
|
} |
267
|
|
|
return $text; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param $string |
272
|
|
|
* @return bool |
273
|
|
|
*/ |
274
|
|
|
public static function isLatinString($string) |
275
|
|
|
{ |
276
|
|
|
return boolval(preg_match_all("/^[\p{Latin}\p{Common}]+$/u", $string)); |
277
|
|
|
//return !$noLatin; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param $string |
282
|
|
|
* @return bool |
283
|
|
|
*/ |
284
|
|
|
public static function isCyrillicString($string) |
285
|
|
|
{ |
286
|
|
|
return boolval(preg_match("/^[\p{Cyrillic}\p{Common}]+$/u", $string)); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param $string |
291
|
|
|
* @return bool |
292
|
|
|
*/ |
293
|
|
|
public static function isAsianString($string) |
294
|
|
|
{ |
295
|
|
|
return boolval(preg_match("/^[\p{Han}\s\p{P}]*$/u", $string)); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* removes all kind of brackets from a given string |
300
|
|
|
* @param $datePart |
301
|
|
|
* @return mixed |
302
|
|
|
*/ |
303
|
|
|
public static function removeBrackets($datePart) |
304
|
|
|
{ |
305
|
|
|
return str_replace(["[", "]", "(", ")", "{", "}"], "", $datePart); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|