Skip to content

Commit 00e3ba0

Browse files
committed
add StringNormalizer
1 parent 8c5662e commit 00e3ba0

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/StringNormalizer.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Utilitte\Php;
4+
5+
final class StringNormalizer
6+
{
7+
8+
/**
9+
* @var string[]
10+
* @copyright https://stackoverflow.com/a/12824140
11+
*/
12+
private static array $emojiRegexes = [
13+
'\x{1F600}-\x{1F64F}', // emoticons,
14+
'\x{1F300}-\x{1F5FF}', // miscellaneous, pictographs
15+
'\x{1F680}-\x{1F6FF}', // transport, map symbols
16+
'\x{2600}-\x{26FF}', // miscellaneous
17+
'\x{2700}-\x{27BF}', // dingbats
18+
];
19+
20+
public static function normalizeSpaces(string $str): string
21+
{
22+
return preg_replace('# {2,}#', ' ', trim($str));
23+
}
24+
25+
public static function normalizeWhitespaces(string $str): string
26+
{
27+
$str = preg_replace('#[\r\n]+#', ' ', trim($str));
28+
29+
return preg_replace('#\s{2,}#', ' ', $str);
30+
}
31+
32+
public static function normalizeEmoji(string $str): string
33+
{
34+
return trim(preg_replace(sprintf('#\s*[%s]+\s*#u', implode('', self::$emojiRegexes)), ' ', $str), ' ');
35+
}
36+
37+
}

tests/cases/stringNormalizer.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types = 1);
2+
3+
use Tester\Assert;
4+
use Utilitte\Php\StringNormalizer;
5+
6+
require __DIR__ . '/../bootstrap.php';
7+
8+
Assert::same('foo bar', StringNormalizer::normalizeEmoji('foo 🎓 bar'));
9+
Assert::same('foo', StringNormalizer::normalizeEmoji('foo 🎓'));
10+
Assert::same('foo bar', StringNormalizer::normalizeEmoji('foo🎓bar'));
11+
Assert::same('bar', StringNormalizer::normalizeEmoji('🎓bar'));
12+
13+
Assert::same('foo bar', StringNormalizer::normalizeSpaces('foo bar'));
14+
Assert::same('foo bar', StringNormalizer::normalizeWhitespaces("foo \r \n bar"));

0 commit comments

Comments
 (0)