skip to Main Content

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


  1. You need to use below function for checking same function for multiple platforms

    function clean($username, $platform) {
        $regex = '';
        switch ($platform) {
            case 'instagram':
                $regex = '/^[a-zA-Z0-9._]{2,30}$/';
                break;
            case 'tiktok':
                $regex = '/^[a-zA-Z0-9._]{2,}$/';
                break;
            default:
                return 'Invalid platform. Please choose either "instagram" or "tiktok".';
        }
    
        if (preg_match($regex, $username)) {
            return "Valid $platform username!";
        } else {
            return "Invalid $platform username!";
        }
    }
    
    Login or Signup to reply.
  2. On this TikTok page I found this requirement for the username:

    Usernames can only contain letters, numbers, underscores, and periods.
    However, periods can’t be put at the end of 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:

    ^(?:(?:https?://)?(?:www.)?tiktok.com/)?@([w.]{0,23}w)(?:/S*)?$
    

    Explanation

    • ^ Start of string
    • (?: Non capture group
      • (?:https?://)? Match optional http:// or https://
      • (?:www.)? Match optional www.
      • tiktok.com/ Match tiktok.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 string

    Regex demo

    If you use a different delimiter than / in PHP, you don’t have to escape the forward slash.

    $pattern = "~^(?:(?:https?://)?(?:www.)?tiktok.com/)?@([w.]{0,23}w)(?:/S*)?$~";
    
    Login or Signup to reply.
  3. 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:

    function parseUsername(string $value): string {
    
        $parsed = parse_url($value);
        // Break paths by / character, drop empty values
        $parsed['paths'] = array_values(array_filter(explode('/', $parsed['path'])));
    
        $username = '';
    
        // Default for all other usernames not included
        $regexp = '/^[a-zA-Z0-9._]{2,50}$/';
        $username = ltrim($parsed['paths'][0] ?? '', '@');
    
        switch(true) {
            case preg_match(
                    '/^(?:(?:www.)?tiktok.com)$/i',
                    $parsed['host'] ?? ''
                ): // tiktok
                // Letters, numbers, periods, underscores. No period at end.
                // Max 24 characters
                $regexp = '/^[a-zA-Z0-9._]{2,24}(?<!.)$/';
                // Username is in first path position, no need to change for tiktok
                break;
            case preg_match(
                    '/^(?:(?:www.)?instagram.com)$/i',
                    $parsed['host'] ?? ''
                ): // instagram
                // Letters, numbers, periods, underscores.
                // Max 30 characters
                $regexp = '/^[a-zA-Z0-9._]{2,30}$/';
                // Username is in first path position, no need to change for insta
                break;
        }
    
        if(preg_match($regexp, $username) == false) {
            // Invalid username found, throw an exception?
            throw new Exception('Invalid username provided.');
        }
    
        // Ensure username starts with @
        return '@' . $username;
    }
    
    var_dump(parseUsername('@coolname1')); // @coolname1
    var_dump(parseUsername('coolname2')); // @coolname2
    var_dump(parseUsername('https://www.instagram.com/mesh1.2')); // @mesh1.2
    var_dump(parseUsername('https://www.tiktok.com/@not_a_valid_name.')); // Throws exception
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search