PHP Remove http, https, www and slashes From URL

Reading Time: 3 minutes
990 Views

Inside this article we will see the concept i.e PHP Remove http, https, www and slashes From URL Example Tutorial. Article contains the classified information about How do I remove http, https and slash from URL in php.

If you are looking for a solution i.e PHP Remove Http Https Www And Slashes From URL then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Read More: Laravel 9 Order By Multiple Columns Example Tutorial

Examples of URLs

https://onlinewebtutorblog.com/, https://api.onlinewebtutorblog.com/

Examples of URLs without https://, www & slash

onlinewebtutorblog.com, api.onlinewebtutorblog.com

Let’s get started.

Application Programming

Create a folder say filter-urls in your localhost directory. Create a file index.php into it.

Method #1 – Remove https:// From URL

Open index.php file and write this code into it.

<?php

// Remove https://
$input = "https://onlinewebtutorblog.com";
$input = preg_replace("#^[^:/.]*[:/]+#i", "", $input);

/* Output onlinewebtutorblog.com */
echo $input;

Output

onlinewebtutorblog.com

Read More: How to Create and Use Laravel 9 Macro Example Tutorial

Method #2 – Remove https://, www. & Slash From URL

<?php

// Remove the https://, www., and slash(/) from the URL 
$input = 'https://www.onlinewebtutorblog.com/';

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

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

$urlParts = parse_url($input);

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

// Output onlinewebtutorblog.com
echo $domain_name;

Output

onlinewebtutorblog.com

Method #3 – Remove https://, www. & Slash From URL (Shorter Method)

<?php

// Short way to do All this one
// Encoded URL to Decode the URL, Remove the http://, www., and slash(/) from the URL 
$input = "https://www.onlinewebtutorblog.com/";  

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

Output

onlinewebtutorblog.com

We hope this article helped you to learn PHP Remove http, https, www, and slashes From URL in a very detailed way.

Read More: Laravel 9 How to get Random Database Records Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.