PHP remove http https www and slashes from URL

Today, We want to share with you PHP remove http https www and slashes from URL.In this post we will show you remove www from url php, hear for Should You Keep or Remove The Trailing Slash on URLs? we will give you demo and example for implement.In this post, we will learn about remove http, https and slash from user input in php with an example.

PHP remove http https www and slashes from URL

There are the Following The simple About How to remove http://, www and slash from URL in PHP? Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP Regex to Remove http:// from string
, so the some major files and Directory structures for this example is following below.

PHP Regex to Remove http:// from string

PHP URL Decode

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

Read Also:  Stripe Refund Api Tutorial Example From Scratch

<?php
    // Decode the Encoded Query URL string
    $user_query_uri = "http%3A%2F%2Fpakainfo.com%2F";
    $user_query_uri = urldecode($user_query_uri);

    // results https://www.pakainfo.com/
    echo $user_query_uri;
?>

Remove http:// from the URL using PHP

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

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

    /* results pakainfo.com */
    echo $user_query_uri;
?>

Add http:// in the URL in PHP

On the other hand, if we want to add http:// in Query URL string, we have to use preg_match() methods. and then, we have to check http:// or https:// existed in Query URL string. If yes then no need to http:// prepend otherwise we have to http:// prepend to Query URL string.

<?php
    //PHP Add http://
    $user_query_uri = 'www.pakainfo.com';

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

    // results http://www.pakainfo.com
    echo $user_query_uri;
?>

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

<?php
    $url = 'http://www.pakainfo.com/example/';

    print_r(parse_url($url));
    echo parse_url($url, PHP_URL_PATH);
?>

Give you An Array

Array
(
    [scheme] => http
    [host] => www.pakainfo.com
    [path] => /example/
)
/example/

<?php
    // Remove the http://, www., and slash(/) from the Query URL string 
    $user_query_uri = 'www.pakainfo.com/';

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

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

    $urlParts = parse_url($user_query_uri);

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

    // results pakainfo.com
    echo $domain_name;
?>

Shorthand way

index.php

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

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

Web Programming Tutorials Example with Demo

Read :

Read Also:  How to delete or clear caching in Laravel?

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about remove http or https from url PHP.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.