Skip to content

Commit fabd4e4

Browse files
committed
Add emailDomain helper
1 parent fc051ed commit fabd4e4

File tree

3 files changed

+88
-35
lines changed

3 files changed

+88
-35
lines changed

README.md

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ AdvStr::redactSsn('My social security number is 222-22-2222'); // My social secu
2525
- [Usage](#usage)
2626
- [Available Methods](#available-methods)
2727
- [advPassword](#advpassword)
28-
- [readTime](#readtime)
2928
- [charWrap](#charwrap)
30-
- [splitName](#splitname)
31-
- [redactSsn](#redactssn)
29+
- [emailDomain](#emaildomain)
30+
- [readTime](#readtime)
3231
- [redactCreditCard](#redactcreditcard)
32+
- [redactSsn](#redactssn)
33+
- [splitName](#splitname)
3334
- [Testing](#testing)
3435
- [Changelog](#changelog)
3536
- [Contributing](#contributing)
@@ -104,58 +105,77 @@ public static function advPassword(
104105
#### Returns:
105106
- string: Generated password
106107

107-
### [readTime](#readtime)
108+
### [charWrap](#charwrap)
108109

109-
Calculates the read time of a string.
110+
Wraps a string at a given number of characters regardless of words.
110111

111112
```php
112-
public static function readTime(
113+
public static function charWrap(
113114
$string,
114-
$wpm = 200
115+
$length = 80
115116
)
116117
```
117118

118119
#### Parameters:
119-
- `$string` (string): The text to calculate read time for
120-
- `$wpm` (int): Words per minute (default: 200)
120+
- `$string` (string): The string to wrap
121+
- `$length` (int): The number of characters to wrap at (default: 80)
121122

122123
#### Returns:
123-
- int: Estimated read time in seconds
124+
- string: The wrapped string
124125

125-
### [charWrap](#charwrap)
126+
### [emailDomain](#emailDomain)
126127

127128
Wraps a string at a given number of characters regardless of words.
128129

129130
```php
130-
public static function charWrap(
131-
$string,
132-
$length = 80
131+
public static function EmailDomain(
132+
$string
133133
)
134134
```
135135

136136
#### Parameters:
137-
- `$string` (string): The string to wrap
138-
- `$length` (int): The number of characters to wrap at (default: 80)
137+
- `$string` (string): The string to extract the email domain from.
139138

140139
#### Returns:
141-
- string: The wrapped string
140+
- string: The email domain from the string
142141

143-
### [splitName](#splitname)
142+
### [readTime](#readtime)
144143

145-
Splits a full name into first name, middle name (if present), and last name, removing any prefixes and suffixes. This method can handle both "Firstname Lastname" and "Lastname, Firstname" formats.
144+
Calculates the read time of a string.
146145

147146
```php
148-
public static function splitName(
149-
$name
147+
public static function readTime(
148+
$string,
149+
$wpm = 200
150150
)
151151
```
152152

153153
#### Parameters:
154-
- `$name` (string): The full name to split
154+
- `$string` (string): The text to calculate read time for
155+
- `$wpm` (int): Words per minute (default: 200)
155156

156157
#### Returns:
157-
- array: An associative array containing 'first', 'middle' (if present), and 'last' name
158+
- int: Estimated read time in seconds
159+
160+
### [redactCreditCard](#redactcreditcard)
161+
Note: This method is currently not implemented (TODO).
162+
Redacts credit card numbers in a string.
163+
164+
```php
165+
public static function redactCreditCard(
166+
$string,
167+
$redacted = '********',
168+
$exclude = []
169+
)
170+
```
171+
172+
#### Parameters:
173+
- `$string` (string): The string containing credit card numbers to redact
174+
- `$redacted` (string): The string to replace credit card numbers with (default: '********')
175+
- `$exclude` (array): An array of credit card types to exclude from redaction
158176

177+
#### Returns:
178+
- string: The string with credit card numbers redacted
159179
### [redactSsn](#redactssn)
160180

161181
Redacts Social Security Numbers (SSN) in a string.
@@ -178,27 +198,21 @@ public static function redactSsn(
178198
#### Returns:
179199
- string: The string with SSNs redacted
180200

181-
### [redactCreditCard](#redactcreditcard)
201+
### [splitName](#splitname)
182202

183-
Redacts credit card numbers in a string.
203+
Splits a full name into first name, middle name (if present), and last name, removing any prefixes and suffixes. This method can handle both "Firstname Lastname" and "Lastname, Firstname" formats.
184204

185205
```php
186-
public static function redactCreditCard(
187-
$string,
188-
$redacted = '********',
189-
$exclude = []
206+
public static function splitName(
207+
$name
190208
)
191209
```
192210

193211
#### Parameters:
194-
- `$string` (string): The string containing credit card numbers to redact
195-
- `$redacted` (string): The string to replace credit card numbers with (default: '********')
196-
- `$exclude` (array): An array of credit card types to exclude from redaction
212+
- `$name` (string): The full name to split
197213

198214
#### Returns:
199-
- string: The string with credit card numbers redacted
200-
201-
Note: This method is currently not implemented (TODO).
215+
- array: An associative array containing 'first', 'middle' (if present), and 'last' name
202216

203217

204218
## Testing

src/AdvStr.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,22 @@ public static function redactSsn($string, $redacted = '********', $dashes = true
259259
return $string;
260260
}
261261

262+
/**
263+
* Extracts the domain from an email address.
264+
*
265+
* @param string $string The email address to extract the domain from.
266+
* @return string The domain extracted from the email address.
267+
*/
268+
public static function emailDomain($string)
269+
{
270+
// Extract the domain using regex
271+
if (preg_match('/@([a-zA-Z0-9.-]+)/', $string, $matches)) {
272+
return $matches[1];
273+
}
274+
275+
return '';
276+
}
277+
262278
/**
263279
* Redacts credit card numbers and replaces them with a given string
264280
*

tests/EmailDomainTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use mpstenson\AdvStr\Facades\AdvStr;
4+
5+
test('Can get email domain', function () {
6+
$domain = AdvStr::emailDomain('[email protected]');
7+
expect($domain)->toBe('example.com');
8+
});
9+
10+
test('Can get email domain with subdomain', function () {
11+
$domain = AdvStr::emailDomain('[email protected]');
12+
expect($domain)->toBe('test.example.com');
13+
});
14+
15+
test('Can get email domain with trailing spaces and text', function () {
16+
$domain = AdvStr::emailDomain('[email protected] some more text');
17+
expect($domain)->toBe('example.com');
18+
});
19+
20+
test('Cat get email domain with not allowed characters afterwards', function () {
21+
$domain = AdvStr::emailDomain('"example user" <[email protected]>');
22+
expect($domain)->toBe('example.com');
23+
});

0 commit comments

Comments
 (0)