Remove http, https, www. and Slashes from URL in PHP

Remove-http-www

Remove http, https, www. and slashes from URL inputted in PHP. When user data (like URL) is get and store into database so at that time need to store a proper URL. So we need to Remove http, https and all slash from URL.

 

Encoded URL to Decode the URL

Encoded URL to Decode URL is encoded character convert into Decode form.

 


<?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 only http:// from user inputted 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:// from the URL

Opposite of above if you want to add http:// with user inputted URL. you have use PHP preg_match() function to check URL already have http:// or https//:. If yes than no need to prepend otherwise condition is false than http:// prepend with 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 slash(/) from the URL

User inputted URL we need only domain host name and store into database than We need to remove http://, www., and slash(/) from inputted URL.

 

We need to use 4 function. First PHP Trim() function is remove all slash from URL, Second preg_match() function to check not have http:// or https:// then prepend it. If you want to need to first check you can use this otherwise leave this function. Third is parse_url() function to parse the URL.

 


<?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.mydomain.com
    [path] => /tutorial/
)
/tutorial/

 

We need to use only host name. And Last fourth preg_replace() function to remove www. from URL.

 


<?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;
?>

 

Short way to do All this one

Encoded URL to Decode the URL, Remove http://, www., and slash(/) from the URL simple and short.

 


<?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 ) ) );  
?>

 
 
Like this article? If you really solve your all confusion “how to Remove http, https and Slashes from URL in PHP” Subscribe to our RSS feed. New article notification sends you via E-Mail or following social share button to share this article.
 
 

Back to Top
About the Post Writer View all posts by
I'm write of this article and founder of Way2tutorial or Webtuts Web Development Programming blog. My passion is keen on contributing WEB Industry. Mostly I do work on PHP, WordPress, Drupal. I hope you enjoy with this articles. Thanks for reading.

2 Comments Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>