Easy way to get Vimeo id from a vimeo url

Easy way to get Vimeo id from a vimeo url

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Stripping Vimeo IDs: A Regexp vs. String Manipulation Showdown

Extracting a video ID from a Vimeo URL is a classic frontend task, often encountered when building embed players or processing media metadata. The standard URL structure is predictable: https://vimeo.com/29474908. While the pattern seems simple enough to assume a basic string slice will suffice, handling edge cases like www. prefixes and optional /clip: segments requires a bit more precision than substr alone can provide.

The code snippet provided in the source material attempts to handle this by first parsing the host and then falling back to a substring operation if a regular expression fails. This approach is fragile. Relying on substr($link, 10) assumes that every valid URL starts exactly at byte index 10 with the ID. It ignores the possibility of subdomains (www.) or query parameters that might alter the length of the prefix. If a new Vimeo feature changes the URL format slightly, this logic breaks silently, returning an incorrect ID or throwing a fatal error when passed to the API.

A more robust solution relies entirely on a well-crafted regular expression. Regular expressions are designed exactly for this: pattern matching within strings. By using preg_match, we can explicitly define the valid components of the URL and capture only what we need, ignoring everything else.

Here is a cleaner, regex-only implementation that handles both standard IDs and clip IDs without needing conditional branches or substring hacks:

function getVimeoId($url)
{
    // Define the pattern to match vimeo.com with optional www and clip prefix
    if (preg_match('~^https?://(?:www\.)?vimeo\.com/(?:clip/)?(\d+)~i', $url, $matches)) {
        return $matches[1];
    }

    return null;
}

This function strips away the complexity of parsing the URL array. It uses a non-capturing group (?:...) to consume the protocol and domain without including them in the result, while (\d+) specifically captures the numeric ID. The i flag ensures case-insensitivity if Vimeo ever switches to uppercase domains. This approach is fail-safe against minor prefix variations because it doesn't rely on fixed character positions.

Once you have the ID, the next step in the original script involves querying the Vimeo API. The provided code uses cURL to fetch data from http://vimeo.com/api/v2/video/$id.php. It's important to note that this specific API endpoint has historically been deprecated or restricted in favor of OAuth 2.0 authentication for most production use cases. However, if you are running a local script or using a public read-only endpoint that hasn't changed, the logic remains valid:

  1. Initialize cURL with CURLOPT_RETURNTRANSFER set to true to get the response as a string instead of printing it directly.
  2. Set a reasonable timeout (CURLOPT_TIMEOUT) to prevent hanging requests if Vimeo's servers are slow or unreachable.
  3. Unserialize the response, which is typically returned as a serialized PHP array by older API versions.

The critical lesson here is separating concerns. The function responsible for extracting the ID should not be coupled with the logic that fetches metadata via cURL. If the API changes its endpoint or requires authentication tomorrow, your ID extraction logic remains untouched and functional. In a Laravel application, you might inject this extractor into a service class or keep it in a utility namespace, ensuring your core business logic isn't burdened with HTTP client details.

For production environments where security and reliability are paramount, always verify the availability of functions like curl_init before attempting to execute requests, as missing extensions will halt script execution immediately. If you need to integrate this into a larger Laravel project, consider checking for the existence of the cURL extension dynamically or wrapping the call in a try-catch block to handle network exceptions gracefully.

For developers looking to streamline their media processing pipelines, understanding these string patterns is fundamental. When dealing with external APIs, always prioritize parsing logic that is resilient to URL format changes over quick-and-dirty substring operations.

Our network