PHP remove http, https, www and slashes from URL

In this article, I show you how to PHP remove http, https, www and slashes from URL inputted. So when user data obtain and store into the database, before that we need to save proper URL. That is the reason we need to remove http, https and slashes from URL.

URL Decode

First of all, encoded URL string we need to decode. For example (+) are decoded to a space character.

<?php
    // Decode the Encoded URL
    $input = "http%3A%2F%2Fway2tutorial.com%2F";
    $input = urldecode($input);

    // Output http://way2tutorial.com/
    echo $input;
?>

Remove http:// from the URL

If you want to remove the only http:// from URL. you have use PHP preg_replace() function.

<?php
    // Remove http://
    $input = "http://way2tutorial.com";
    $input = preg_replace( "#^[^:/.]*[:/]+#i", "", $input );

    /* Output way2tutorial.com */
    echo $input;
?>

Add http:// in the URL

On the other hand, if you want to add http:// in URL, you have to use preg_match() function. First, you have to check http:// or https:// existed in URL. If yes then no need to http:// prepend otherwise you have to http:// prepend to URL.

<?php
    // Add http://
    $input = 'www.way2tutorial.com';

    // Check, if not have http:// or https:// then prepend it
    if (!preg_match('#^http(s)?://#', $input)) {
        $input = 'http://' . $input;
    }

    // Output http://www.way2tutorial.com
    echo $input;
?>

Remove http://, www., and slashes from the URL

If we need only domain hostname and store that into out database then we need to remove http://, www., and slash(/) from URL. So for that, when need to use four functions.

  • First is trim() function, use for remove all slash from the URL.
  • Second is preg_match() function, check http:// or https:// existed in URL. If yes then no need to http:// prepend otherwise you have to http:// prepend to URL.
  • Third is parse_url() function, to parse the URL.
  • And last forth preg_replace() function to remove www. from URL because we need only hostname.
<?php
    $url = 'http://www.way2tutorial.com/tutorial/';

    print_r(parse_url($url));

    echo parse_url($url, PHP_URL_PATH);
?>

Give you Array

Array
(
    [scheme] => http
    [host] => www.way2tutorial.com
    [path] => /tutorial/
)
/tutorial/
<?php
    // Remove the http://, www., and slash(/) from the URL 
    $input = 'www.way2tutorial.com/';

    // If URI is like, eg. www.way2tutorial.com/
    $input = trim($input, '/');

    // If not have http:// or https:// then prepend it
    if (!preg_match('#^http(s)?://#', $input)) {
        $input = 'http://' . $input;
    }

    $urlParts = parse_url($input);

    // Remove www.
    $domain_name = preg_replace('/^www\./', '', $urlParts['host']);

    // Output way2tutorial.com
    echo $domain_name;
?>

Shorthand

To all above in shorthand way you can do following.

<?php
    // Short way to do All this one
    // Encoded URL to Decode the URL, Remove the http://, www., and slash(/) from the URL 
    $input = "http%3A%2F%2Fway2tutorial.com%2F";  

    // Output way2tutorial.com      
    echo preg_replace( "#^[^:/.]*[:/]+#i", "", preg_replace( "{/$}", "", urldecode( $input ) ) );  
?>

That’s all. If any query or confusion, please write down comment on below comment section. Moreover, you can explore here other PHP related posts.

If you like our article, please consider buying a coffee for us.
Thanks for your support!

Support us on Buy me a coffee! Buy me a coffee!



1 Response Leave a Comment

  1. This trick works well! Thanks!

Join the Discussion.