Skip to content

Commit 13c0100

Browse files
authored
Implement FilterURL class for Telegram URL validation
This class provides functionality to validate and parse Telegram URLs, returning structured data or error messages.
1 parent e495af0 commit 13c0100

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

src/FilterURL.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/*
3+
the project created by @wizardloop
4+
*/
5+
6+
namespace Wizardloop\TelegramUrlParser;
7+
8+
class FilterURL
9+
{
10+
public static function checkUrl(string $url): array
11+
{
12+
if (!filter_var($url, FILTER_VALIDATE_URL)) {
13+
return ['error' => 'invalid url!'];
14+
}
15+
16+
if (!preg_match('/^http(s)?:\/\/t\.me\/.+\/?$/i', $url)) {
17+
return ['error' => 'invalid url!'];
18+
}
19+
20+
$result = self::parseUrl($url);
21+
22+
if ($result !== null) {
23+
return $result;
24+
}
25+
26+
return ['error' => 'invalid url!'];
27+
}
28+
29+
private static function parseUrl(string $url): ?array
30+
{
31+
$path = parse_url($url, PHP_URL_PATH);
32+
33+
if (empty($path)) {
34+
return null;
35+
}
36+
37+
$segments = explode('/', trim($path, '/'));
38+
39+
return [
40+
'out1' => $segments[0] ?? null,
41+
'out2' => $segments[1] ?? null,
42+
'out3' => $segments[2] ?? null,
43+
'out4' => $segments[3] ?? null,
44+
'out5' => $segments[4] ?? null,
45+
];
46+
}
47+
}
48+
49+
/* # usage example:
50+
use Wizardloop\TelegramUrlParser\filterURL;
51+
$check = filterURL::checkUrl("https://t.me/something");
52+
$out1 = $check['out1'] ?? null;
53+
$out2 = $check['out2'] ?? null;
54+
$out3 = $check['out3'] ?? null;
55+
$out4 = $check['out4'] ?? null;
56+
$out5 = $check['out5'] ?? null;
57+
58+
if(isset($check['error'])){
59+
$error = $check['error'] ?? 'null'; # invalid url error
60+
}else{
61+
62+
if(!preg_match('/^\+/',$out1)){
63+
if ($out5 != null) {
64+
# invalid url
65+
}else{
66+
if ($out1 === 'c' || $out1 === 'C') {
67+
if($out4 != null){
68+
# invalid url
69+
}else{
70+
$out2 // id chat
71+
$out3 // id message
72+
73+
# GROUP / CHANNEL
74+
# PRIVATE
75+
76+
// .... your logic here
77+
}
78+
79+
}elseif($out1 === 'b' || $out1 === 'B') {
80+
$out2 // id chat
81+
$out3 // id message
82+
83+
# BOT
84+
85+
// .... your logic here
86+
87+
}elseif($out1 === 'u' || $out1 === 'U') {
88+
$out2 // id chat
89+
$out3 // id message
90+
# USER
91+
92+
// .... your logic here
93+
94+
}else{
95+
96+
if($out3 != null){
97+
# invalid format
98+
}else{
99+
$out1 // username
100+
$out2 // id message
101+
102+
# GROUP / CHANNEL
103+
# PUBLIC
104+
105+
// .... your logic here
106+
107+
}
108+
109+
}
110+
111+
}
112+
113+
}
114+
if(preg_match('/^\+/',$out1)){
115+
# invalid url
116+
}
117+
118+
}
119+
*/
120+
121+
/* # url examples:
122+
channel post public chat: https://t.me/username/id # username channel + channel post id
123+
channel post private chat: https://t.me/c/id/id # id channel + channel post id
124+
channel comment public chat: https://t.me/username/id # Discussion Group Username + Message ID
125+
channel comment private chat: https://t.me/c/id/id # Discussion Group ID + Message ID
126+
group message public chat: https://t.me/username/id # username group + message id
127+
group message private chat: https://t.me/c/id/id # id group + message id
128+
bot message: https://t.me/b/username/id # username bot + message id
129+
user message by username: https://t.me/u/username/id # username of user + message id
130+
user message by id: https://t.me/u/id/id # user id + message id
131+
topics group message public chat: https://t.me/username/id # username group + message id (remove topic id)
132+
topics group message private chat: https://t.me/c/id/id # id group + message id (remove topic id)
133+
*/

0 commit comments

Comments
 (0)