Parsing Username from Social Media URLs with PHP

1 min read .

In web development, it’s often necessary to extract usernames from social media URLs for purposes like data analysis or content personalization. Will guide you through a PHP function designed to extract usernames from URLs of various social media platforms such as Twitter, Medium, Facebook, Vimeo, and Instagram.

Function parseUsername

The parseUsername function is designed to take a URL as input and return the username found in that URL. Below is the implementation of the function:

function parseUsername(string $url): string
{
    $output = $url;

    // Parse username
    preg_match('/(?:https?:\/\/)?(?:www\.)?(?:twitter|medium|facebook|vimeo|instagram)(?:\.com\/)?([@a-zA-Z0-9-_]+)/im', $url, $matches);

    // Set output
    $output = count($matches) ? $matches[1] : $output;

    return $output;
}

Explanation of the Code

  1. Initialize Output Variable:

    $output = $url;
    • The $output variable is initialized with the value of the $url parameter. This will be used to store the final result of the function.
  2. Extract Username Using preg_match:

    preg_match('/(?:https?:\/\/)?(?:www\.)?(?:twitter|medium|facebook|vimeo|instagram)(?:\.com\/)?([@a-zA-Z0-9-_]+)/im', $url, $matches);
    • preg_match is used to search for a pattern within the $url string. The pattern used here searches for usernames from common social media platforms.
    • Regular Expression Pattern:
      • (?:https?:\/\/)? - Handles the HTTP or HTTPS protocol (optional).
      • (?:www\.)? - Handles the “www” subdomain (optional).
      • (?:twitter|medium|facebook|vimeo|instagram) - Matches the domain name of relevant social media platforms.
      • (?:\.com\/)? - Matches the “.com” domain and a slash (optional).
      • ([@a-zA-Z0-9-_]+) - Captures the username, which can contain letters, numbers, underscores, and hyphens. Usernames may start with “@” if present.
  3. Set Output:

    $output = count($matches) ? $matches[1] : $output;
    • If a match is found (count($matches) is greater than 0), the username ($matches[1]) is used as the final result. If no match is found, the output remains the same as the original URL.
  4. Return Result:

    return $output;

Example Usage

Here are some examples of using the parseUsername function:

echo parseUsername("https://twitter.com/johndoe");
// Output: johndoe

echo parseUsername("https://www.instagram.com/jane_doe/");
// Output: jane_doe

echo parseUsername("https://facebook.com/profile.php?id=1000123456789");
// Output: profile.php?id=1000123456789
  • Example 1: The Twitter URL "https://twitter.com/johndoe" returns the username "johndoe".
  • Example 2: The Instagram URL "https://www.instagram.com/jane_doe/" returns the username "jane_doe".
  • Example 3: The Facebook URL does not return the expected username due to its different format.

Conclusion

The parseUsername function is a useful tool for extracting usernames from social media URLs. By using regular expressions, this function can handle various URL formats from popular social media platforms. This allows you to easily retrieve the necessary user information from given URLs, improving efficiency in web applications that require integration with multiple social media platforms.

Tags:
PHP

See Also

chevron-up