Skip to content

Commit c34e5a1

Browse files
committed
add string cleaner
1 parent d8c12ed commit c34e5a1

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
"prefer-stable": true,
1111
"require-dev": {
1212
"phpunit/phpunit": "^9.5"
13+
},
14+
"require": {
15+
"ext-mbstring": "*"
1316
}
1417
}

src/StrCleaner.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @author <julienrajerison5@gmail.com>
4+
*/
5+
6+
namespace Julkwel\StringTools;
7+
8+
/**
9+
* Class StrCleaner.
10+
*/
11+
class StrCleaner
12+
{
13+
/**
14+
* Clean special char and convert it to lowercase.
15+
* Param should be a valid string
16+
*
17+
* ref : https://stackoverflow.com/a/14114443/10462455
18+
*
19+
* @param string $stringToClean
20+
*
21+
* @return string
22+
*/
23+
public static function cleanString(string $stringToClean): string
24+
{
25+
$utf8 = [
26+
'/[áàâãªä]/u' => 'a',
27+
'/[ÁÀÂÃÄ]/u' => 'A',
28+
'/[ÍÌÎÏ]/u' => 'I',
29+
'/[íìîï]/u' => 'i',
30+
'/[éèêë]/u' => 'e',
31+
'/[ÉÈÊË]/u' => 'E',
32+
'/[óòôõºö]/u' => 'o',
33+
'/[ÓÒÔÕÖ]/u' => 'O',
34+
'/[úùûü]/u' => 'u',
35+
'/[ÚÙÛÜ]/u' => 'U',
36+
'/ç/' => 'c',
37+
'/Ç/' => 'C',
38+
'/ñ/' => 'n',
39+
'/Ñ/' => 'N',
40+
'/–/' => '-', // UTF-8 hyphen to "normal" hyphen
41+
'/[’‘‹›‚]/u' => ' ', // Literally a single quote
42+
'/[“”«»„]/u' => ' ', // Double quote
43+
'/ /' => ' ', // non-breaking space (equiv. to 0x160)
44+
];
45+
46+
return mb_strtolower(preg_replace(array_keys($utf8), array_values($utf8), $stringToClean));
47+
}
48+
}

src/StrCompare.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,28 @@ class StrCompare
1717
*
1818
* @return bool
1919
*/
20-
public function isStringIsEquals(...$strings)
20+
public static function isStringIsEquals(...$strings): bool
2121
{
22-
$prevString = [];
22+
$results = [];
2323
foreach ($strings as $string) {
24-
$prevString[$string] = $string;
24+
$results[$string] = $string;
2525
}
2626

27-
return count($prevString) === 1;
27+
return count($results) === 1;
28+
}
29+
30+
/**
31+
* @param ...$strings
32+
*
33+
* @return bool
34+
*/
35+
public static function compareCleanString(...$strings): bool
36+
{
37+
$results = [];
38+
foreach ($strings as $string) {
39+
$results[StrCleaner::cleanString($string)] = $string;
40+
}
41+
42+
return count($results) === 1;
2843
}
2944
}

0 commit comments

Comments
 (0)