trimming www. and http:// best practices


This question already has an answer here:


The way trim works is that it trims each individual character (www. is the same as .w).

You're looking for preg_replace with a regex of ^(https?://)?(www\.)?:

$website = preg_replace('~^(https?://)?(www\.)?~i', '', $website);

Debuggex Demo


  • ^ the match MUST start with whatever comes after this (makes sure that we only replace if the match is in the start)
  • (https?://)?
    • http - the literal string http
    • s? - an optional s (in case we use https)
    • :// - the literal string ://
    • ? - makes the whole thing optional
  • (www\.)?
    • www\. - the literal string www. (you need to escape the . to \. as . means "any character")
    • ? - makes the whole thing optional
  • i - this is the modifier, and i makes the whole thing in case sensitive (will match HTTP and http)

Regex 101 Demo

KIS: Keep It Simple.

From the docs:

$url = 'http://username:password@hostname/path?arg=value#anchor';


echo parse_url($url, PHP_URL_PATH);

    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor

EDIT: PHP Getting Domain Name From Subdomain When you have the host.

Classic use case for RegEx. This snippet removes http(s) and www prefixes.

$new_url = preg_replace('/(?:https?://)?(?:www.)?(.*)/?$/i', '$1', $url);

Need Your Help

How do I free SQLServerCE's COM instance from the current running process?

c++ sql com sql-server-ce

It's been a while since I touched COM so be nice ;) This is under WindowsCE 5.0 with SQLServerCE 2.0.