PHP Regex to Remove http:// from string

PHP Regex to Remove http:// from string

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Protocol Prefixes in PHP URLs

When processing full URLs as strings, you often encounter the need to strip the protocol scheme (like http:// or https://) to generate a cleaner display version, such as turning http://www.example.com into www.example.com. While this might seem trivial at first glance, achieving it requires careful consideration of regex patterns and string parsing logic to avoid breaking edge cases like relative paths or malformed URLs.

The Regex Approach

The most direct solution involves using regular expressions to locate and replace the protocol prefix. In PHP, you can utilize preg_replace with a pattern that matches http:// or https:// followed by any characters until the start of the domain name.

Here is a basic implementation:

$url = 'http://www.google.com';
$cleanUrl = preg_replace('/^https?:\/\//i', '', $url);
echo $cleanUrl; // Outputs: www.google.com

The pattern /^https?:\/\//i breaks down as follows:

  • ^ asserts the start of the string.
  • https? matches either http or https.
  • :\/\/ matches the literal characters ://. Note the escaped forward slashes, which are standard in regex patterns.
  • The i flag makes the matching case-insensitive, ensuring that HTTP:// is also caught.

This approach works well for simple cases where you strictly want to remove the leading protocol. However, relying solely on regex introduces fragility. If your input string contains extra whitespace, a trailing slash immediately after the domain (which you might wish to keep or remove), or if the URL is missing a scheme entirely, the regex behavior remains consistent but the result might not align with user expectations.

Parsing vs. Regex

While regex is efficient for quick replacements, PHP's filter_var function combined with filter_return offers a more robust alternative for handling URLs. Functions like filter_var($url, FILTER_SANITIZE_URL) can validate and normalize URLs, though in modern PHP versions, strict sanitization practices have evolved. A more reliable programmatic approach is to use the parse_url function.

By extracting specific components of the URL, you can reconstruct it without the scheme:

$url = 'http://www.google.com';
$parsed = parse_url($url);

if (isset($parsed['host'])) {
    $cleanUrl = $parsed['host'];
} else {
    // Fallback or error handling if parsing fails
    $cleanUrl = preg_replace('/^https?:\/\//i', '', $url);
}

echo $cleanUrl; // Outputs: www.google.com

Using parse_url ensures that you are dealing with the actual host component rather than relying on pattern matching. This method naturally handles edge cases where the scheme might be missing or malformed, as it returns an array of components or false if parsing fails. It is generally safer for production code where data integrity matters.

Display Considerations and Links

If your goal is to display these URLs in HTML, simply stripping the protocol from the text content often leaves you with a plain link that lacks context. Developers frequently face the dilemma of showing www.google.com as text while maintaining a clickable link to http://www.google.com.

In many cases, stripping the scheme from the visible text is acceptable for UI cleanliness, but you must ensure the underlying href attribute still points to the full URL. For example:

<a href="http://www.google.com">www.google.com</a>

This pattern creates a visual disconnect between the link target and the displayed text, which can be confusing for users expecting the display text to match the destination exactly. If you strip the http:// from the string using the methods above, ensure you are only modifying the string intended for display, not the data source used for navigation.

Conclusion on Implementation

Choosing between regex and parsing functions depends on your specific constraints. Regex provides a concise one-liner suitable for lightweight scripts or quick transformations where input validation is already handled upstream. Parsing with parse_url offers better type safety and clarity regarding what parts of the URL are being manipulated. Always validate the input before applying transformations to prevent unexpected behavior with malformed URLs.

Our network