mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-12-23 21:43:17 +00:00
Add Nickname Lexer
This commit is contained in:
parent
7e78261081
commit
63c665cdc9
1 changed files with 38 additions and 0 deletions
38
app/Util/Lexer/Nickname.php
Normal file
38
app/Util/Lexer/Nickname.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace App\Util\Lexer;
|
||||
|
||||
class Nickname {
|
||||
|
||||
public static function normalizeProfileUrl($url)
|
||||
{
|
||||
if(!str_contains($url, '@') && filter_var($url, FILTER_VALIDATE_URL)) {
|
||||
$parsed = parse_url($url);
|
||||
$username = str_replace(['/','\\','@'], '', $parsed['path']);
|
||||
return ['domain' => $parsed['host'], 'username' => $username];
|
||||
}
|
||||
$parts = explode('@', $url);
|
||||
$username = null;
|
||||
$domain = null;
|
||||
|
||||
foreach ($parts as $part) {
|
||||
|
||||
// skip empty array slices
|
||||
if(empty($part)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// if slice contains . assume its a domain
|
||||
if(str_contains($part, '.')) {
|
||||
$domain = filter_var($part, FILTER_VALIDATE_URL) ?
|
||||
parse_url($part, PHP_URL_HOST) :
|
||||
$part;
|
||||
} else {
|
||||
$username = $part;
|
||||
}
|
||||
|
||||
}
|
||||
return ['domain' => $domain, 'username' => $username];
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue