I am adding individual form for individual social media link (Instagram, Twitter and Tiktok)
I am struggling to find the right pattern to validate Tiktok username. Below is my pregmatch code for Instagram. Can anyone suggest me what will be the pattern for TIKTOK. The form must accept both the whole url and or just the handle. i.e if someone provides just the handle @xyz or the whole url www.tiktok.cpm/@xyz – the form must validate both.
For Instagram I have written the following regex code
public function validate($value) {
$value = $this->clean($value);
// Validate as Instagram username (1-30 word characters, starts with @,
return preg_match('/^@w(?:(?:w|(?:.(?!.))){0,28}(?:w))?$/', $value);
}
FYI The function clean is like below
public function clean($value) {
// Tolerate a link
$url_matches = [];
if (preg_match('/^https?://(?:www.)?instagram.com/([w.]+)/?$/', $value, $url_matches)) {
$value = '@' . $url_matches[1];
}
// Prepend with @ if not already present
if (!preg_match('/^@/', $value)) {
$value = '@' . $value;
}
return $value;
}
Edited
TikTok usernames have a 24-character limit. The handle will automatically be preceded by an “@” symbol. Usernames can only contain letters, numbers, periods and underscores.
Example Tiktok account – https://www.tiktok.com/@imperialcollegeunion/
Example Instagram account – https://www.instagram.com/icunion/
3
Answers
You need to use below function for checking same function for multiple platforms
On this TikTok page I found this requirement for the username:
If you state that the username can have a maximum of 24 characters (I assume a minimum length of 1 character) you can match 0-23 of the allowed chars including a dot, and exclude the dot at the end of the username.
Note that if you only provide the handle, and the same handle is valid for instagram, you would have to determine the precedence.
I am not sure about all the available TikTok urls, but if you want to match the whole url using anchors, and the http(s) and the www. part are optional:
Explanation
^
Start of string(?:
Non capture group(?:https?://)?
Match optional http:// or https://(?:www.)?
Match optional www.tiktok.com/
Matchtiktok.com/
)?
Close the non capture group and make it optional@
Match literally([w.]{0,23}w)
(?:/S*)?
Optionally match/
and optional non whitespace chars$
End of stringRegex demo
If you use a different delimiter than
/
in PHP, you don’t have to escape the forward slash.You can parse the string as a URL (don’t worry, strings with just usernames will still parse and be added as a path as long as they don’t contain any URL-specific characters) using
parse_url
: