1
+ <?php
2
+
3
+
4
+ namespace doganoo \PHPUtil \Service ;
5
+
6
+ /**
7
+ * Class Regex
8
+ * @package doganoo\PHPUtil\Service
9
+ */
10
+ class Regex {
11
+ public const PHONE_NUMBERS_REGEX = "<^(( \\+|00)[1-9] \\d{0,3}|0 ?[1-9]| \\(00? ?[1-9][ \\d ]* \\))[ \\d \\-/ ]*$> " ;
12
+ public const UNWANTED_CHARS_IN_PHONE_STRING = "/[^0-9 \\+ \\( \\) \\- \\/]/ " ;
13
+
14
+ /**
15
+ * strips all email adresses out of an string. More information at:
16
+ * https://stackoverflow.com/questions/1028553/how-to-get-email-address-from-a-long-string
17
+ *
18
+ * @param string $dump
19
+ * @param bool $unique
20
+ * @return array
21
+ */
22
+ public function stripAddresses (string $ dump , bool $ unique = true ): array {
23
+ $ emails = [];
24
+ foreach (preg_split ('/\s/ ' , $ dump ) as $ token ) {
25
+ $ email = filter_var (filter_var ($ token , FILTER_SANITIZE_EMAIL ), FILTER_VALIDATE_EMAIL );
26
+ if ($ email !== false ) {
27
+ $ emails [] = $ email ;
28
+ }
29
+ }
30
+ if ($ unique ) {
31
+ $ emails = \array_unique ($ emails );
32
+ }
33
+ return $ emails ;
34
+ }
35
+
36
+ /**
37
+ * @param string $dump
38
+ * @param bool $unique
39
+ * @return array
40
+ */
41
+ public function stripPhoneNumbers (string $ dump , bool $ unique ): array {
42
+
43
+ $ numbers = [];
44
+
45
+ $ delimiter = "; " ;
46
+ $ dump = \str_replace (" " , "" , $ dump );
47
+ $ res = preg_replace (Regex::UNWANTED_CHARS_IN_PHONE_STRING , $ delimiter , $ dump );
48
+ $ array = \explode ($ delimiter , $ res );
49
+
50
+ foreach ($ array as $ value ) {
51
+ if (\preg_match (Regex::PHONE_NUMBERS_REGEX , $ value )) {
52
+ $ value = preg_replace ("<^ \\+> " , "00 " , $ value );
53
+ $ value = preg_replace ("< \\D+> " , "" , $ value );
54
+ $ numbers [] = $ value ;
55
+ }
56
+ }
57
+
58
+ if ($ unique ) {
59
+ $ numbers = \array_unique ($ numbers );
60
+ }
61
+ return $ numbers ;
62
+ }
63
+ }
0 commit comments