Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
UrlTransliterate.php
1 <?php
3 {
4  public static function cleanString($string, $separator = '-',$cleanPunctuation = TRUE, $cleanSlash = TRUE)
5  {
6  // Default words to ignore
7  $ignoreWords = array(
8  'a', 'an', 'as', 'at', 'before', 'but', 'by', 'for', 'from', 'is', 'in',
9  'into', 'like', 'of', 'off', 'on', 'onto', 'per', 'since', 'than', 'the',
10  'this', 'that', 'to', 'up', 'via', 'with',
11  );
12 
13  $output = $string;
14  if ($cleanPunctuation)
15  {
16  $punctuation = self::punctuationChars();
17  foreach ($punctuation as $name => $details)
18  {
19  // Slightly tricky inline if which either replaces with the separator or nothing
20  $output = str_replace($details['value'], $separator, $output);
21  }
22  }
23 
24  // If something is already urlsafe then don't remove slashes
25  if ($cleanSlash)
26  {
27  $output = str_replace('/', '', $output);
28  }
29 
30  // Optionally remove accents and transliterate
31  $translations = self::i18nToAscII();
32  $output = strtr($output, $translations);
33 
34  // Reduce to the subset of ASCII96 letters and numbers
35  //$pattern = '/[^a-zA-Z0-9\/]+/ ';
36  //$output = preg_replace($pattern, $separator, $output);
37 
38  // Get rid of words that are on the ignore list
39  $ignoreRe = '\b'. preg_replace('/,/', '\b|\b', $ignoreWords) .'\b';
40 
41  if (function_exists('mb_eregi_replace'))
42  {
43  $output = mb_eregi_replace($ignoreRe, '', $output);
44  }
45  else
46  {
47  $output = preg_replace("/$ignoreRe/i", '', $output);
48  }
49 
50  // Always replace whitespace with the separator.
51  $output = preg_replace('/\s+/', $separator, $output);
52 
53  // Trim duplicates and remove trailing and leading separators.
54  $output = self::cleanSeparators($output, $separator);
55 
56  // Enforce the maximum component length
57  //$maxlength = 128;
58  //$output = substr($output, 0, $maxlength);
59 
60  return $output;
61 
62  }
63 
64  /**
65  * Clean path separators from a given string.
66  *
67  * Trims duplicates and strips leading and trailing separators.
68  *
69  * @param $string
70  * The string to clean path separators from.
71  * @param $separator
72  * The path separator to use when cleaning.
73  * @return
74  * The cleaned version of the string.
75  *
76  * @see pathauto_cleanstring()
77  * @see pathauto_clean_alias()
78  */
79  public static function cleanSeparators($string, $separator = NULL, $toLowCase = TRUE)
80  {
81  $output = $string;
82 
83  // Clean duplicate or trailing separators.
84  if (isset($separator) && strlen($separator))
85  {
86  // Escape the separator.
87  $seppattern = preg_quote($separator, '/');
88 
89  // Trim any leading or trailing separators.
90  $output = preg_replace("/^$seppattern+|$seppattern+$/", '', $output);
91 
92  // Replace trailing separators around slashes.
93  $output = preg_replace("/$seppattern+\/|\/$seppattern+/", "/", $output);
94 
95  // Replace multiple separators with a single one.
96  $output = preg_replace("/$seppattern+/", $separator, $output);
97  }
98 
99  // Optionally convert to lower case.
100  if ($toLowCase)
101  {
102  $output = strtolower($output);
103  }
104 
105  return $output;
106  }
107 
108  /**
109  * Return an array of arrays for punctuation values.
110  *
111  * Returns an array of arrays for punctuation values keyed by a name, including
112  * the value and a textual description.
113  * Can and should be expanded to include "all" non text punctuation values.
114  *
115  * @return
116  * An array of arrays for punctuation values keyed by a name, including the
117  * value and a textual description.
118  */
119  public static function punctuationChars()
120  {
121  // Handle " ' ` , . - _ : ; | { [ } ] + = * & % ^ $ # @ ! ~ ( ) ? < > \
122  return array(
123  "double_quotes"=>'"',
124  "quotes"=>"'",
125  "backtick"=>"`",
126  "comma"=>",",
127  "period"=>".",
128  "hyphen"=>"-",
129  "underscore"=>"_",
130  "colon"=>":",
131  "semicolon"=>";",
132  "pipe"=>"|",
133  "left_curly"=>"{",
134  "left_square"=>"[",
135  "right_curly"=>"}",
136  "right_square"=>"]",
137  "plus"=>"+",
138  "equal"=>"=",
139  "asterisk"=>"*",
140  "ampersand"=>"&",
141  "percent"=>"%",
142  "caret"=>"^",
143  "dollar"=>"$",
144  "hash"=>"#",
145  "at"=>"@",
146  "exclamation"=>"!",
147  "tilde"=>"~",
148  "left_parenthesis"=>"(",
149  "right_parenthesis"=>")",
150  "question_mark"=>"?",
151  "less_than"=>"<",
152  "greater_than"=>">",
153  "back_slash"=>'\\',
154  );
155  }
156 
157  public static function i18nToAscII()
158  {
159  return array(
160  "À" => "A",
161  "Á" => "A",
162  "Â" => "A",
163  "Ã" => "A",
164  "Ä" => "Ae",
165  "Å" => "A",
166  "Æ" => "A",
167  "Ā" => "A",
168  "Ą" => "A",
169  "Ă" => "A",
170  "Ç" => "C",
171  "Ć" => "C",
172  "Č" => "C",
173  "Ĉ" => "C",
174  "Ċ" => "C",
175  "Ď" => "D",
176  "Đ" => "D",
177  "È" => "E",
178  "É" => "E",
179  "Ê" => "E",
180  "Ë" => "E",
181  "Ē" => "E",
182  "Ę" => "E",
183  "Ě" => "E",
184  "Ĕ" => "E",
185  "Ė" => "E",
186  "Ĝ" => "G",
187  "Ğ" => "G",
188  "Ġ" => "G",
189  "Ģ" => "G",
190  "Ĥ" => "H",
191  "Ħ" => "H",
192  "Ì" => "I",
193  "Í" => "I",
194  "Î" => "I",
195  "Ï" => "I",
196  "Ī" => "I",
197  "Ĩ" => "I",
198  "Ĭ" => "I",
199  "Į" => "I",
200  "İ" => "I",
201  "IJ" => "IJ",
202  "Ĵ" => "J",
203  "Ķ" => "K",
204  "Ľ" => "K",
205  "Ĺ" => "K",
206  "Ļ" => "K",
207  "Ŀ" => "K",
208  "Ł" => "L",
209  "Ñ" => "N",
210  "Ń" => "N",
211  "Ň" => "N",
212  "Ņ" => "N",
213  "Ŋ" => "N",
214  "Ò" => "O",
215  "Ó" => "O",
216  "Ô" => "O",
217  "Õ" => "O",
218  "Ö" => "Oe",
219  "Ø" => "O",
220  "Ō" => "O",
221  "Ő" => "O",
222  "Ŏ" => "O",
223  "Œ" => "OE",
224  "Ŕ" => "R",
225  "Ř" => "R",
226  "Ŗ" => "R",
227  "Ś" => "S",
228  "Ş" => "S",
229  "Ŝ" => "S",
230  "Ș" => "S",
231  "Š" => "S",
232  "Ť" => "T",
233  "Ţ" => "T",
234  "Ŧ" => "T",
235  "Ț" => "T",
236  "Ù" => "U",
237  "Ú" => "U",
238  "Û" => "U",
239  "Ü" => "Ue",
240  "Ū" => "U",
241  "Ů" => "U",
242  "Ű" => "U",
243  "Ŭ" => "U",
244  "Ũ" => "U",
245  "Ų" => "U",
246  "Ŵ" => "W",
247  "Ŷ" => "Y",
248  "Ÿ" => "Y",
249  "Ý" => "Y",
250  "Ź" => "Z",
251  "Ż" => "Z",
252  "Ž" => "Z",
253  "à" => "a",
254  "á" => "a",
255  "â" => "a",
256  "ã" => "a",
257  "ä" => "ae",
258  "ā" => "a",
259  "ą" => "a",
260  "ă" => "a",
261  "å" => "a",
262  "æ" => "ae",
263  "ç" => "c",
264  "ć" => "c",
265  "č" => "c",
266  "ĉ" => "c",
267  "ċ" => "c",
268  "ď" => "d",
269  "đ" => "d",
270  "è" => "e",
271  "é" => "e",
272  "ê" => "e",
273  "ë" => "e",
274  "ē" => "e",
275  "ę" => "e",
276  "ě" => "e",
277  "ĕ" => "e",
278  "ė" => "e",
279  "ƒ" => "f",
280  "ĝ" => "g",
281  "ğ" => "g",
282  "ġ" => "g",
283  "ģ" => "g",
284  "ĥ" => "h",
285  "ħ" => "h",
286  "ì" => "i",
287  "í" => "i",
288  "î" => "i",
289  "ï" => "i",
290  "ī" => "i",
291  "ĩ" => "i",
292  "ĭ" => "i",
293  "į" => "i",
294  "ı" => "i",
295  "ij" => "ij",
296  "ĵ" => "j",
297  "ķ" => "k",
298  "ĸ" => "k",
299  "ł" => "l",
300  "ľ" => "l",
301  "ĺ" => "l",
302  "ļ" => "l",
303  "ŀ" => "l",
304  "ñ" => "n",
305  "ń" => "n",
306  "ň" => "n",
307  "ņ" => "n",
308  "ʼn" => "n",
309  "ŋ" => "n",
310  "ò" => "o",
311  "ó" => "o",
312  "ô" => "o",
313  "õ" => "o",
314  "ö" => "oe",
315  "ø" => "o",
316  "ō" => "o",
317  "ő" => "o",
318  "ŏ" => "o",
319  "œ" => "oe",
320  "ŕ" => "r",
321  "ř" => "r",
322  "ŗ" => "r",
323  "ś" => "s",
324  "š" => "s",
325  "ş" => "s",
326  "ť" => "t",
327  "ţ" => "t",
328  "ù" => "u",
329  "ú" => "u",
330  "û" => "u",
331  "ü" => "ue",
332  "ū" => "u",
333  "ů" => "u",
334  "ű" => "u",
335  "ŭ" => "u",
336  "ũ" => "u",
337  "ų" => "u",
338  "ŵ" => "w",
339  "ÿ" => "y",
340  "ý" => "y",
341  "ŷ" => "y",
342  "ż" => "z",
343  "ź" => "z",
344  "ž" => "z",
345  "ß" => "ss",
346  "ſ" => "ss",
347  "Α" => "A",
348  "Ά" => "A",
349  "Ἀ" => "A",
350  "Ἁ" => "A",
351  "Ἂ" => "A",
352  "Ἃ" => "A",
353  "Ἄ" => "A",
354  "Ἅ" => "A",
355  "Ἆ" => "A",
356  "Ἇ" => "A",
357  "ᾈ" => "A",
358  "ᾉ" => "A",
359  "ᾊ" => "A",
360  "ᾋ" => "A",
361  "ᾌ" => "A",
362  "ᾍ" => "A",
363  "ᾎ" => "A",
364  "ᾏ" => "A",
365  "Ᾰ" => "A",
366  "Ᾱ" => "A",
367  "Ὰ" => "A",
368  "Ά" => "A",
369  "ᾼ" => "A",
370  "Β" => "B",
371  "Γ" => "G",
372  "Δ" => "D",
373  "Ε" => "E",
374  "Έ" => "E",
375  "Ἐ" => "E",
376  "Ἑ" => "E",
377  "Ἒ" => "E",
378  "Ἓ" => "E",
379  "Ἔ" => "E",
380  "Ἕ" => "E",
381  "Έ" => "E",
382  "Ὲ" => "E",
383  "Ζ" => "Z",
384  "Η" => "I",
385  "Ή" => "I",
386  "Ἠ" => "I",
387  "Ἡ" => "I",
388  "Ἢ" => "I",
389  "Ἣ" => "I",
390  "Ἤ" => "I",
391  "Ἥ" => "I",
392  "Ἦ" => "I",
393  "Ἧ" => "I",
394  "ᾘ" => "I",
395  "ᾙ" => "I",
396  "ᾚ" => "I",
397  "ᾛ" => "I",
398  "ᾜ" => "I",
399  "ᾝ" => "I",
400  "ᾞ" => "I",
401  "ᾟ" => "I",
402  "Ὴ" => "I",
403  "Ή" => "I",
404  "ῌ" => "I",
405  "Θ" => "TH",
406  "Ι" => "I",
407  "Ί" => "I",
408  "Ϊ" => "I",
409  "Ἰ" => "I",
410  "Ἱ" => "I",
411  "Ἲ" => "I",
412  "Ἳ" => "I",
413  "Ἴ" => "I",
414  "Ἵ" => "I",
415  "Ἶ" => "I",
416  "Ἷ" => "I",
417  "Ῐ" => "I",
418  "Ῑ" => "I",
419  "Ὶ" => "I",
420  "Ί" => "I",
421  "Κ" => "K",
422  "Λ" => "L",
423  "Μ" => "M",
424  "Ν" => "N",
425  "Ξ" => "KS",
426  "Ο" => "O",
427  "Ό" => "O",
428  "Ὀ" => "O",
429  "Ὁ" => "O",
430  "Ὂ" => "O",
431  "Ὃ" => "O",
432  "Ὄ" => "O",
433  "Ὅ" => "O",
434  "Ὸ" => "O",
435  "Ό" => "O",
436  "Π" => "P",
437  "Ρ" => "R",
438  "Ῥ" => "R",
439  "Σ" => "S",
440  "Τ" => "T",
441  "Υ" => "Y",
442  "Ύ" => "Y",
443  "Ϋ" => "Y",
444  "Ὑ" => "Y",
445  "Ὓ" => "Y",
446  "Ὕ" => "Y",
447  "Ὗ" => "Y",
448  "Ῠ" => "Y",
449  "Ῡ" => "Y",
450  "Ὺ" => "Y",
451  "Ύ" => "Y",
452  "Φ" => "F",
453  "Χ" => "X",
454  "Ψ" => "PS",
455  "Ω" => "O",
456  "Ώ" => "O",
457  "Ὠ" => "O",
458  "Ὡ" => "O",
459  "Ὢ" => "O",
460  "Ὣ" => "O",
461  "Ὤ" => "O",
462  "Ὥ" => "O",
463  "Ὦ" => "O",
464  "Ὧ" => "O",
465  "ᾨ" => "O",
466  "ᾩ" => "O",
467  "ᾪ" => "O",
468  "ᾫ" => "O",
469  "ᾬ" => "O",
470  "ᾭ" => "O",
471  "ᾮ" => "O",
472  "ᾯ" => "O",
473  "Ὼ" => "O",
474  "Ώ" => "O",
475  "ῼ" => "O",
476  "α" => "a",
477  "ά" => "a",
478  "ἀ" => "a",
479  "ἁ" => "a",
480  "ἂ" => "a",
481  "ἃ" => "a",
482  "ἄ" => "a",
483  "ἅ" => "a",
484  "ἆ" => "a",
485  "ἇ" => "a",
486  "ᾀ" => "a",
487  "ᾁ" => "a",
488  "ᾂ" => "a",
489  "ᾃ" => "a",
490  "ᾄ" => "a",
491  "ᾅ" => "a",
492  "ᾆ" => "a",
493  "ᾇ" => "a",
494  "ὰ" => "a",
495  "ά" => "a",
496  "ᾰ" => "a",
497  "ᾱ" => "a",
498  "ᾲ" => "a",
499  "ᾳ" => "a",
500  "ᾴ" => "a",
501  "ᾶ" => "a",
502  "ᾷ" => "a",
503  "β" => "b",
504  "γ" => "g",
505  "δ" => "d",
506  "ε" => "e",
507  "έ" => "e",
508  "ἐ" => "e",
509  "ἑ" => "e",
510  "ἒ" => "e",
511  "ἓ" => "e",
512  "ἔ" => "e",
513  "ἕ" => "e",
514  "ὲ" => "e",
515  "έ" => "e",
516  "ζ" => "z",
517  "η" => "i",
518  "ή" => "i",
519  "ἠ" => "i",
520  "ἡ" => "i",
521  "ἢ" => "i",
522  "ἣ" => "i",
523  "ἤ" => "i",
524  "ἥ" => "i",
525  "ἦ" => "i",
526  "ἧ" => "i",
527  "ᾐ" => "i",
528  "ᾑ" => "i",
529  "ᾒ" => "i",
530  "ᾓ" => "i",
531  "ᾔ" => "i",
532  "ᾕ" => "i",
533  "ᾖ" => "i",
534  "ᾗ" => "i",
535  "ὴ" => "i",
536  "ή" => "i",
537  "ῂ" => "i",
538  "ῃ" => "i",
539  "ῄ" => "i",
540  "ῆ" => "i",
541  "ῇ" => "i",
542  "θ" => "th",
543  "ι" => "i",
544  "ί" => "i",
545  "ϊ" => "i",
546  "ΐ" => "i",
547  "ἰ" => "i",
548  "ἱ" => "i",
549  "ἲ" => "i",
550  "ἳ" => "i",
551  "ἴ" => "i",
552  "ἵ" => "i",
553  "ἶ" => "i",
554  "ἷ" => "i",
555  "ὶ" => "i",
556  "ί" => "i",
557  "ῐ" => "i",
558  "ῑ" => "i",
559  "ῒ" => "i",
560  "ΐ" => "i",
561  "ῖ" => "i",
562  "ῗ" => "i",
563  "κ" => "k",
564  "λ" => "l",
565  "μ" => "m",
566  "ν" => "n",
567  "ξ" => "ks",
568  "ο" => "o",
569  "ό" => "o",
570  "ὀ" => "o",
571  "ὁ" => "o",
572  "ὂ" => "o",
573  "ὃ" => "o",
574  "ὄ" => "o",
575  "ὅ" => "o",
576  "ὸ" => "o",
577  "ό" => "o",
578  "π" => "p",
579  "ρ" => "r",
580  "ῤ" => "r",
581  "ῥ" => "r",
582  "σ" => "s",
583  "ς" => "s",
584  "τ" => "t",
585  "υ" => "y",
586  "ύ" => "y",
587  "ϋ" => "y",
588  "ΰ" => "y",
589  "ὐ" => "y",
590  "ὑ" => "y",
591  "ὒ" => "y",
592  "ὓ" => "y",
593  "ὔ" => "y",
594  "ὕ" => "y",
595  "ὖ" => "y",
596  "ὗ" => "y",
597  "ὺ" => "y",
598  "ύ" => "y",
599  "ῠ" => "y",
600  "ῡ" => "y",
601  "ῢ" => "y",
602  "ΰ" => "y",
603  "ῦ" => "y",
604  "ῧ" => "y",
605  "φ" => "f",
606  "χ" => "x",
607  "ψ" => "ps",
608  "ω" => "o",
609  "ώ" => "o",
610  "ὠ" => "o",
611  "ὡ" => "o",
612  "ὢ" => "o",
613  "ὣ" => "o",
614  "ὤ" => "o",
615  "ὥ" => "o",
616  "ὦ" => "o",
617  "ὧ" => "o",
618  "ᾠ" => "o",
619  "ᾡ" => "o",
620  "ᾢ" => "o",
621  "ᾣ" => "o",
622  "ᾤ" => "o",
623  "ᾥ" => "o",
624  "ᾦ" => "o",
625  "ᾧ" => "o",
626  "ὼ" => "o",
627  "ώ" => "o",
628  "ῲ" => "o",
629  "ῳ" => "o",
630  "ῴ" => "o",
631  "ῶ" => "o",
632  "ῷ" => "o",
633  "¨" => "",
634  "΅" => "",
635  "᾿" => "",
636  "῾" => "",
637  "῍" => "",
638  "῝" => "",
639  "῎" => "",
640  "῞" => "",
641  "῏" => "",
642  "῟" => "",
643  "῀" => "",
644  "῁" => "",
645  "΄" => "",
646  "΅" => "",
647  "`" => "",
648  "῭" => "",
649  "ͺ" => "",
650  "᾽" => "",
651  "А" => "A",
652  "Б" => "B",
653  "В" => "V",
654  "Г" => "G",
655  "Д" => "D",
656  "Е" => "E",
657  "Ё" => "E",
658  "Ж" => "ZH",
659  "З" => "Z",
660  "И" => "I",
661  "Й" => "I",
662  "К" => "K",
663  "Л" => "L",
664  "М" => "M",
665  "Н" => "N",
666  "О" => "O",
667  "П" => "P",
668  "Р" => "R",
669  "С" => "S",
670  "Т" => "T",
671  "У" => "U",
672  "Ф" => "F",
673  "Х" => "KH",
674  "Ц" => "TS",
675  "Ч" => "CH",
676  "Ш" => "SH",
677  "Щ" => "SHCH",
678  "Ы" => "Y",
679  "Э" => "E",
680  "Ю" => "YU",
681  "Я" => "YA",
682  "а" => "A",
683  "б" => "B",
684  "в" => "V",
685  "г" => "G",
686  "д" => "D",
687  "е" => "E",
688  "ё" => "E",
689  "ж" => "ZH",
690  "з" => "Z",
691  "и" => "I",
692  "й" => "I",
693  "к" => "K",
694  "л" => "L",
695  "м" => "M",
696  "н" => "N",
697  "о" => "O",
698  "п" => "P",
699  "р" => "R",
700  "с" => "S",
701  "т" => "T",
702  "у" => "U",
703  "ф" => "F",
704  "х" => "KH",
705  "ц" => "TS",
706  "ч" => "CH",
707  "ш" => "SH",
708  "щ" => "SHCH",
709  "ы" => "Y",
710  "э" => "E",
711  "ю" => "YU",
712  "я" => "YA",
713  "Ъ" => "",
714  "ъ" => "",
715  "Ь" => "",
716  "ь" => "",
717  "ð" => "d",
718  "Ð" => "D",
719  "þ" => "th",
720  "Þ" => "TH"
721  );
722  }
723 }