Skip to content

Commit ae229ad

Browse files
committed
Regex class
1 parent 7829188 commit ae229ad

File tree

3 files changed

+183
-1
lines changed

3 files changed

+183
-1
lines changed

composer.lock

Lines changed: 119 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Mail/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Class Validator
3030
*
3131
* @package doganoo\PHPUtil\Mail
32+
* @deprecated use Service/Regex instead
3233
*/
3334
class Validator {
3435
/**

src/Service/Regex.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)