Skip to content

Commit e495af0

Browse files
authored
Clean up filterURL.php by removing comments
Removed commented-out code and unnecessary explanations.
1 parent 3463813 commit e495af0

File tree

1 file changed

+73
-72
lines changed

1 file changed

+73
-72
lines changed

filterURL.php

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,92 @@
11
<?php
2+
/*
3+
the project created by @wizardloop
4+
*/
25

3-
/*
4-
echo "Magic";
5-
Function to filter the chat type by telegram url with custom filters: (b/u)
6+
namespace Wizardloop\TelegramUrlParser;
67

7-
all type of chats: channel/group/user/bot || public/private || channel comment & topics groups
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+
}
815

9-
For all supported formats, go to the examples folder.
16+
if (!preg_match('/^http(s)?:\/\/t\.me\/.+\/?$/i', $url)) {
17+
return ['error' => 'invalid url!'];
18+
}
1019

11-
( From my project: @GetAnyMessageRobot )
20+
$result = self::parseUrl($url);
1221

13-
# Written by PHPwiz ( php-wiz )
22+
if ($result !== null) {
23+
return $result;
24+
}
1425

15-
# explanation:
16-
$out1 = $result1['out1'] ?? null; // PATH URL 1
17-
$out2 = $result1['out2'] ?? null; // PATH URL 2
18-
$out3 = $result1['out3'] ?? null; // PATH URL 3
19-
$out4 = $result1['out4'] ?? null; // PATH URL 4
20-
$out5 = $result1['out5'] ?? null; // PATH URL 5
26+
return ['error' => 'invalid url!'];
27+
}
2128

22-
PATH URL 1 ($out1):
23-
if path is c/C = private chat(group/channel)
24-
if path is u/U = user chat
25-
if path is b/B = chat bot
26-
else = (username) so its public channel/group
29+
private static function parseUrl(string $url): ?array
30+
{
31+
$path = parse_url($url, PHP_URL_PATH);
2732

28-
*checks if path does not start with + to filter out invitation links.
29-
*Check only on Telegram links.
33+
if (empty($path)) {
34+
return null;
35+
}
3036

31-
*/
37+
$segments = explode('/', trim($path, '/'));
3238

33-
$text = 'your_telegram_link';
34-
35-
if(preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i',$text)){ // valid telegram url
36-
if (!preg_match('/^http(s)?:\/\/t\.me\/.+\/?$/i', $text)) {
37-
# invalid format
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+
];
3846
}
39-
if (preg_match('/^http(s)?:\/\/t\.me\/.+\/?$/i', $text)) {
40-
41-
if(!function_exists("extractTelegramPaths")){
42-
function extractTelegramPaths($url) {
43-
$path = parse_url($url, PHP_URL_PATH);
44-
if (empty($path)) {
45-
return null;
46-
}
47-
$segments = explode('/', trim($path, '/'));
48-
$out1 = isset($segments[0]) ? $segments[0] : null;
49-
$out2 = isset($segments[1]) ? $segments[1] : null;
50-
$out3 = isset($segments[2]) ? $segments[2] : null;
51-
$out4 = isset($segments[3]) ? $segments[3] : null;
52-
$out5 = isset($segments[4]) ? $segments[4] : null;
53-
return [
54-
'out1' => $out1,
55-
'out2' => $out2,
56-
'out3' => $out3,
57-
'out4' => $out4,
58-
'out5' => $out5,
59-
];
6047
}
61-
}
62-
$result1 = extractTelegramPaths($text);
63-
64-
if ($result1 === null) {
65-
# invalid format
66-
} else {
67-
$out1 = $result1['out1'] ?? null; // PATH URL 1
68-
$out2 = $result1['out2'] ?? null; // PATH URL 2
69-
$out3 = $result1['out3'] ?? null; // PATH URL 3
70-
$out4 = $result1['out4'] ?? null; // PATH URL 4
71-
$out5 = $result1['out5'] ?? null; // PATH URL 5
7248

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{
7361
7462
if(!preg_match('/^\+/',$out1)){
75-
7663
if ($out5 != null) {
77-
# invalid format
64+
# invalid url
7865
}else{
7966
if ($out1 === 'c' || $out1 === 'C') {
80-
8167
if($out4 != null){
82-
# invalid format
68+
# invalid url
8369
}else{
84-
$out2 = $result1['out2'] ?? null; // id chat
85-
$out3 = $result1['out3'] ?? null; // id message
70+
$out2 // id chat
71+
$out3 // id message
72+
8673
# GROUP / CHANNEL
8774
# PRIVATE
8875
8976
// .... your logic here
9077
}
9178
9279
}elseif($out1 === 'b' || $out1 === 'B') {
93-
$out3 = $result1['out3'] ?? null; // id message
80+
$out2 // id chat
81+
$out3 // id message
82+
9483
# BOT
9584
9685
// .... your logic here
9786
9887
}elseif($out1 === 'u' || $out1 === 'U') {
99-
$out3 = $result1['out3'] ?? null; // id message
88+
$out2 // id chat
89+
$out3 // id message
10090
# USER
10191
10292
// .... your logic here
@@ -106,8 +96,8 @@ function extractTelegramPaths($url) {
10696
if($out3 != null){
10797
# invalid format
10898
}else{
109-
$out1 = $result1['out1'] ?? null; // username
110-
$out2 = $result1['out2'] ?? null; // id message
99+
$out1 // username
100+
$out2 // id message
111101
112102
# GROUP / CHANNEL
113103
# PUBLIC
@@ -122,11 +112,22 @@ function extractTelegramPaths($url) {
122112
123113
}
124114
if(preg_match('/^\+/',$out1)){
125-
# invalid format
126-
}
115+
# invalid url
127116
}
128117
129118
}
130-
}//else{//This is not a Telegram link.}
119+
*/
131120

132-
?>
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)