check if a string is a URL

check if a string is a URL

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

Stefan Bogdanescu

Founder & Senior Architect

Validating URLs Without JavaScript

Determining whether a user-provided string represents a valid URL requires parsing its structure against established standards. The core challenge lies in distinguishing between plain text that merely contains domain-like characters and strings that conform to the syntax defined by RFC 3986 and subsequent specifications. When working in server-side environments where client-side JavaScript might be disabled or blocked, relying on regex patterns or dedicated validation functions becomes the primary defense mechanism.

A common misconception is that a string like something.com qualifies as a full URL. While it contains a hostname, a strict definition of a Uniform Resource Locator usually requires a scheme (such as http, https, ftp) and often a path component to be considered a complete address. If your application logic treats something.com as invalid without the protocol prefix, you must enforce that requirement in your validation function. Conversely, if you intend to treat any domain-like string as a valid target regardless of the scheme, your pattern needs to be more permissive.

The Regex Approach

In PHP, the most straightforward way to implement this logic without external libraries is using regular expressions. You can replace the placeholder IsUrl function with a pattern that matches common URL structures. This approach avoids dependencies and works even when JavaScript is unavailable on the client side or when processing raw input streams.

Here is a practical implementation that checks for a scheme followed by a domain:

function IsUrl($url) {
    // Matches http, https, ftp, etc., followed by a domain name
    $pattern = '/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/i';
    
    return preg_match($pattern, $url) === 1;
}

$text = "something.com"; 

if (!IsUrl($text)) {
    echo "No it is not url";
    exit; 
} else {
    echo "Yes it is url";
    // Proceed with your logic
}

This pattern enforces the presence of a scheme optionally (due to the ?) but ensures the domain structure exists. However, regex validation has limitations. It can struggle with edge cases involving internationalized domain names (IDN), IPv6 addresses enclosed in brackets, or URLs containing special characters in the query string that require percent-encoding. If your application needs to handle complex URL formats, a simple regex might produce false negatives or positives depending on how strict the standard is enforced.

Using Stream Filters for Strict Validation

For scenarios requiring higher accuracy without writing custom regex patterns, PHP provides filter_var() with the FILTER_VALIDATE_URL constant. This method delegates the validation logic to the internal C library implementations that adhere to RFC standards. It handles edge cases like IPv6 addresses and proper query string formatting much better than a hand-written regular expression.

function IsUrl($url) {
    return filter_var($url, FILTER_VALIDATE_URL) !== false;
}

This approach is generally preferred in production environments because it reduces the risk of introducing bugs through custom pattern maintenance. It returns false for invalid URLs and a string containing the original URL if valid. Note that this function will reject strings like something.com unless you explicitly strip the leading http:// before validation, or if you modify the filter flags to be less strict, which can reintroduce security concerns.

Security Considerations

Regardless of the method chosen, never trust a URL string solely based on its validity check when it originates from user input. A valid URL could still point to a malicious resource. If your application redirects users or fetches data based on this input, ensure you sanitize and validate the scheme specifically (e.g., allowing only https for sensitive operations). Additionally, be wary of phishing attempts that use homograph attacks; while regex and filters catch syntax errors, they cannot inherently verify ownership of a domain without additional reputation checks.

If you are building a larger application framework where URL handling is critical, exploring robust packages or consulting resources like https://laravelcompany.com can provide insights into how established frameworks manage routing and validation safely at scale.

Our network