Total Complexity | 8 |
Total Lines | 43 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
11 | class Text |
||
12 | { |
||
13 | |||
14 | public static $usernamesPattern = '/(?:@)([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)/i'; |
||
15 | public static $tagsPattern = '/(?:#)([\p{L}0-9_](?:(?:[\p{L}0-9_]){0,28}(?:[\p{L}0-9_]))?)/ui'; |
||
16 | |||
17 | /** |
||
18 | * @param string $text |
||
19 | * @param int $minLength |
||
20 | * @return array|null |
||
21 | * |
||
22 | * @see http://blog.jstassen.com/2016/03/code-regex-for-instagram-username-and-hashtags/ |
||
23 | */ |
||
24 | public static function getTags(string $text, $minLength = 2): ?array |
||
25 | { |
||
26 | if (preg_match_all(static::$tagsPattern, mb_strtolower($text), $matches)) { |
||
27 | |||
28 | $tags = array_filter($matches['1'], function ($tag) use ($minLength) { |
||
29 | if ($minLength === false || is_int($minLength) && mb_strlen($tag) >= $minLength) { |
||
30 | return true; |
||
31 | } |
||
32 | }); |
||
33 | |||
34 | return array_values(array_unique($tags))?:null; |
||
35 | } |
||
36 | |||
37 | return null; |
||
38 | |||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param string $text |
||
43 | * @return array|null |
||
44 | * |
||
45 | * @see http://blog.jstassen.com/2016/03/code-regex-for-instagram-username-and-hashtags/ |
||
46 | */ |
||
47 | public static function getUsernames(string $text): ?array |
||
54 | } |
||
55 | } |