-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_accented_characters.php
35 lines (33 loc) · 1.27 KB
/
convert_accented_characters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
/**
* Converts accented characters to ASCII
* @param string $str
* @param string $delimiter
* @return string
*
*
* Examples:
* Input: "José compró una vieja zampoña en Perú. Excusándose, Sofía tiró su whisky al desagüe de la banqueta."
* Output: "jose-compro-una-vieja-zampona-en-peru-excusandose-sofia-tiro-su-whisky-al-desague-de-la-banqueta"
* Input: "В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!"
* Output: "v-casah-uga-zil-by-citrus-da-no-fal-sivyj-ekzemplar"
* Input: "A quick brown fox jumps over the lazy dog."
* Output: "a-quick-brown-fox-jumps-over-the-lazy-dog"*
*
* Advanced transliterator library:
* @link https://github.com/martinille/transliteratorPlus
*
*
* @author Martin Ille
* @email ille.martin@gmail.com
* @since PHP 5.6+
* @dependencies:
* - transliterator extension (required)
* - mbstring extension (required)
*/
function convertAccentedCharacters($str, $delimiter = '-') {
$ret = transliterator_transliterate('Any-Latin; Latin-ASCII', $str);
$ret = preg_replace("/[^[:alnum:]]+/", $delimiter, $ret);
$ret = mb_strtolower(trim($ret));
return (string)preg_replace(["/" . $delimiter . "+/", "/" . $delimiter . "+$/", "/^" . $delimiter . "+/"], [$delimiter, "", ""], $ret);
}