Skip to content

Commit a40c06f

Browse files
Utility and TextSanitizer class added
1 parent 8d2d352 commit a40c06f

File tree

6 files changed

+186
-21
lines changed

6 files changed

+186
-21
lines changed

README.md

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,24 @@ Support Package For PHP and Laravel
8181
* [last](#last)
8282
* [TextSanitizer](#TextSanitizer)
8383
* [Usage](#sanitizer-usage)
84-
* [phone](#phone)
84+
* [phoneInt](#phoneInt)
85+
* [phoneWords](#phoneWords)
86+
* [url](#sanitize-url)
87+
* [email](#sanitize-email)
88+
* [mention](#sanitize-mention)
89+
* [hastag](#sanitize-hastag)
90+
* [sanitize](#sanitize)
91+
* [findPhoneWords](#findPhoneWords)
92+
* [Utility](#Utility)
93+
* [Usage](#utility-usage)
94+
* [phoneInt](#phoneInt)
95+
* [phoneWords](#phoneWords)
96+
* [url](#sanitize-url)
97+
* [email](#sanitize-email)
98+
* [mention](#sanitize-mention)
99+
* [hastag](#sanitize-hastag)
100+
* [sanitize](#sanitize)
101+
* [findPhoneWords](#findPhoneWords)
85102
* [Country](#country)
86103
* [Usage](#country-usage)
87104
* [iso3](#country-iso3)
@@ -609,6 +626,91 @@ Str::encrypt('secret'); // encrypted
609626
Str::decrypt('...'); // original
610627
```
611628

629+
## TextSanitizer
630+
- The Core Class For String Sanitizer
631+
- It's helper class can be called, using -- `TameSanitizer()`
632+
633+
### Sanitizer Usage
634+
```php
635+
use Tamedevelopers\Support\TextSanitizer;
636+
637+
$text = "My number is 0812300089 and another is zero eight one two three four five six seven.
638+
Visit https://x.com google.com or mail me at [email protected] +23400000209
639+
I love #coding with @friends. I also have two cats. 11092092-3";
640+
641+
$text2 = "I'm a beautiful zero nine zero and sweet in seven five seven eight available from ";
642+
$text3 = "Reach me on zero eight one double two 555 nine or 0812220000";
643+
$text4 = "Visit https://x.com google.com or mail me at [email protected] @username.com. +23400";
644+
645+
646+
$sanitizer->phoneInt($text); //
647+
$sanitizer->phoneWords($text2); // I'm a beautiful [phone] available from
648+
$sanitizer->phoneWords($text3); // Reach me on [phone]
649+
$sanitizer->url($text4); // Visit [url] [url] or mail me at [email protected] @username.com. +23400
650+
$sanitizer->email($text4); // Visit https://x.com google.com or mail me at [email] @username.com. +23400
651+
$sanitizer->mention($text4); // Visit https://x.com google.com or mail me at [email protected] [username]. +23400
652+
$sanitizer->findPhoneWords($text); // Collect all numbers both in words as return as int in array
653+
// array [
654+
// 0 => "0812300089"
655+
// 1 => "081234567"
656+
// 2 => "23400000209"
657+
// 3 => "2110920923"
658+
// ]
659+
660+
$sanitizer->sanitize($text); // sanitize all in a go!
661+
// My number is [phone] and another is [phone].
662+
// Visit [url] [url] or mail me at [email]. +[phone]
663+
// I love [hastag] with [mention]. I also have two cats. [phone]-3
664+
665+
// sanitize(
666+
// string $text,
667+
// array $rules = [
668+
// 'phoneInt' => '[phone]',
669+
// 'phoneWords' => '[phone]',
670+
// 'url' => '[url]',
671+
// 'email' => '[email]',
672+
// 'mention' => '[mention]',
673+
// 'hastag' => '[hastag]',
674+
// ]
675+
// );
676+
```
677+
678+
## Utility
679+
- The Core Class For Email and String manipulations analysiss
680+
- It's helper class can be called, using -- `TameUtility()`
681+
682+
### Utility Usage
683+
```php
684+
use Tamedevelopers\Support\Utility;
685+
686+
$text = "
687+
The Christ the Redeemer Sanctuary, located in the heart of Rio de Janeiro, is the world’s
688+
first open-air sanctuary, offering a sacred space welcoming people of all cultures,
689+
beliefs, and backgrounds.";
690+
691+
$email = "[email protected]";
692+
$email2 = "[email protected]";
693+
$email3 = "[email protected]";
694+
695+
$util = Utility::text($text);
696+
697+
$util->readingTime(); // 10 seconds
698+
$util->wordCount(); // 32
699+
$util->charCount(); // 207
700+
$util->sentenceCount(); // 1
701+
$util->reverse(); // used to reverse the text words
702+
Utility::text('This is a new text')->readingTime(); // 2 seconds
703+
Utility::text("A man, a plan, a canal: Panama")->isPalindrome(); //true
704+
705+
706+
$util->maskEmail($email); // *********[email protected]
707+
Utility::normalizeEmail($email); // [email protected]
708+
Utility::normalizeEmail($email2); // [email protected]
709+
Utility::normalizeEmail($email3); // [email protected]
710+
Utility::isGmail($email2); // true
711+
Utility::validateEmail($email); // true
712+
```
713+
612714
## Country
613715
- Country data and helpers
614716
- Class: `Tamedevelopers\Support\Country`

src/TextSanitizer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ public static function email(string $text, string $replacer = "[email]")
197197
*/
198198
public static function mention(string $text, string $replacer = "[username]")
199199
{
200-
$pattern = '/@\w{3,}/';
200+
// Negative lookbehind ensures @ is not part of an email (no word or dot before it)
201+
$pattern = '/(?<![\w.])@([A-Za-z0-9_.]{3,})\b/';
202+
201203
return preg_replace($pattern, $replacer, $text);
202204
}
203205

src/Utility.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,28 @@ public function __toString()
9393
{
9494
return $this->getText();
9595
}
96+
97+
/**
98+
* Magic: customize what is displayed during var-dump/dd().
99+
* Provides a pretty, safe snapshot of the current class.
100+
*
101+
* - text: the text input
102+
* - readingTime: reading time using 200 words per minute
103+
* - wordCount: Count words in text.
104+
* - charCount: Count words in text.
105+
* - sentenceCount: Count text sentences (approximate) by splitting on punctuation.
106+
*
107+
* @return array
108+
*/
109+
public function __debugInfo(): array
110+
{
111+
return [
112+
'text' => $this->getText(),
113+
'readingTime' => $this->readingTime(),
114+
'wordCount' => $this->wordCount(),
115+
'charCount' => $this->charCount(),
116+
'sentenceCount' => $this->sentenceCount(),
117+
];
118+
}
119+
96120
}

src/helpers.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
use Tamedevelopers\Support\Cookie;
1414
use Tamedevelopers\Support\Server;
1515
use Tamedevelopers\Support\Country;
16+
use Tamedevelopers\Support\Utility;
1617
use Tamedevelopers\Support\Translator;
1718
use Tamedevelopers\Support\NumberToWords;
19+
use Tamedevelopers\Support\TextSanitizer;
1820
use Tamedevelopers\Support\Capsule\Manager;
1921
use Tamedevelopers\Support\Process\Session;
2022
use Tamedevelopers\Support\AutoloadRegister;
@@ -156,6 +158,31 @@ function TameStr()
156158
}
157159
}
158160

161+
if (! function_exists('TameSanitizer')) {
162+
/**
163+
* Tame Sanitizer
164+
*
165+
* @return \Tamedevelopers\Support\TextSanitizer
166+
*/
167+
function TameSanitizer()
168+
{
169+
return new TextSanitizer();
170+
}
171+
}
172+
173+
if (! function_exists('TameUtility')) {
174+
/**
175+
* Tame Utility
176+
*
177+
* @param string|null $text
178+
* @return \Tamedevelopers\Support\Utility
179+
*/
180+
function TameUtility($text = null)
181+
{
182+
return new Utility($text);
183+
}
184+
}
185+
159186
if (! function_exists('TameCountry')) {
160187
/**
161188
* Country Class

tests/textsanitizer.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66

77
$sanitizer = new TextSanitizer();
88

9-
$text = "My number is 08123456789 and another is zero eight one two three four five six seven.
10-
Visit https://example.com google.com or mail me at [email protected]. +23409109209
9+
$text = "My number is 0812300089 and another is zero eight one two three four five six seven.
10+
Visit https://x.com google.com or mail me at [email protected]. +23400000209
1111
I love #coding with @friends. I also have two cats. 11092092-3";
1212

1313
$text2 = "I'm a beautiful zero nine zero and sweet in seven five seven eight available from ";
14-
$text3 = "Reach me on zero eight one double two 555 nine or 0812225559";
14+
$text3 = "Reach me on zero eight one double two 555 nine or 0812220000";
15+
$text4 = "Visit https://x.com google.com or mail me at [email protected] @username.com. +23400";
1516

1617
dd(
1718
$sanitizer->phoneInt($text),
1819
$sanitizer->phoneWords($text2),
1920
$sanitizer->phoneWords($text3),
20-
$sanitizer->url($text),
21+
$sanitizer->url($text4),
22+
$sanitizer->email($text4),
23+
$sanitizer->mention($text4),
24+
$sanitizer->hastag($text4),
2125
$sanitizer->sanitize($text),
2226
$sanitizer->findPhoneWords($text),
2327
);

tests/utility.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
// --------------------------
99
// Example usage
1010
// --------------------------
11+
1112
$text = "
12-
The Christ the Redeemer Sanctuary, located in the heart of Rio de Janeiro, is the world’s first open-air sanctuary,
13-
offering a sacred space welcoming people of all cultures, beliefs, and backgrounds.
14-
More than just a monument, it is a symbol of faith, hope, and hospitality, inviting millions of visitors each year
13+
The Christ the Redeemer Sanctuary, located in the heart of Rio de Janeiro, is the world’s
14+
first open-air sanctuary, offering a sacred space welcoming people of all cultures,
15+
beliefs, and backgrounds.";
16+
17+
$text2 = $text . "More than just a monument, it is a symbol of faith, hope, and hospitality, inviting millions of visitors each year
1518
for moments of reflection, spirituality, and connection with nature.
1619
1720
Built atop Corcovado Mountain at 710 meters above sea level, Christ the Redeemer is one of the Seven Wonders of the Modern World.
@@ -24,29 +27,32 @@
2427

2528

2629
$util = Utility::text($text);
27-
$email = "maxwell+freeman@xd.org";
28-
$email2 = "maxwell.freeman@gmail.com";
29-
$email3 = "freeman[email protected]";
30+
$email = "maxwell+example@xd.org";
31+
$email2 = "maxwell.example@gmail.com";
32+
$email3 = "example[email protected]";
3033

3134
// echo($util) . PHP_EOL;
3235

3336
dump(
34-
Utility::maskEmail($email),
37+
$util->maskEmail($email),
3538
[
3639
Utility::normalizeEmail($email),
3740
Utility::normalizeEmail($email2),
3841
Utility::normalizeEmail($email3),
3942
"isGmail: " . (Utility::isGmail($email2) ? 'yes' : 'no'),
43+
Utility::equalsEmail($email, $email),
4044
],
41-
Utility::equalsEmail($email, $email, true),
42-
// Utility::validateEmail($email, true, true),
4345

46+
[
47+
Utility::text('This is a new text')->readingTime(),
48+
$util->readingTime(),
49+
$util->wordCount(),
50+
$util->charCount(),
51+
$util->sentenceCount(),
52+
Utility::text("A man, a plan, a canal: Panama")->isPalindrome()
53+
],
54+
$util,
55+
// Utility::validateEmail($email, true, true),
4456
// $util->getText(),
45-
Utility::text('This is a new text')->readingTime(),
46-
$util->readingTime(),
47-
$util->wordCount(),
48-
$util->charCount(),
49-
$util->sentenceCount(),
50-
Utility::text("A man, a plan, a canal: Panama")->isPalindrome(),
5157
// $util->reverse(),
5258
);

0 commit comments

Comments
 (0)