Open In App

How to add http:// if it doesn’t exists in the URL in PHP?

There are many approaches to add http:// in the URL if it doesn’t exist. Some of them are discussed below.
Method 1: Using preg_match() function: This function searches string for pattern and returns true if pattern exists, otherwise returns false. Usually, the search starts from the beginning of the subject string. The optional parameter offset is used to specify the position from where to start the search.

Syntax:



int preg_match($pattern, $string, $pattern_array, $flags, $offset )

Return value: It returns true if pattern exists, otherwise false.

Example 1: This example takes the URL without http:// and returns the complete URL.




<?php
  
// Function to add http
function addHttp($url) {
      
    // Search the pattern
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
          
        // If not exist then add http
        $url = "http://" . $url;
    }
      
    // Return the URL
    return $url;
}
  
// Declare a variable and initialize 
// it with URL
$url = "geeksforgeeks.org";
  
// Display URL
echo $url;
echo "\n";
  
// Display URL with http
echo addHttp($url);
  
?>

Output:

geeksforgeeks.org
http://geeksforgeeks.org

Example 2: This example takes the URL with http:// and returns the URL without doing any correction.




<?php
  
// Function to add http
function addHttp($url) {
      
    // Search the pattern
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
          
        // If not exist then add http
        $url = "http://" . $url;
    }
      
    // Return the URL
    return $url;
}
  
// Declare a variable and initialize 
// it with URL
  
// Display URL
echo $url;
echo "\n";
  
// Display URL with http
echo addHttp($url);
  
?>

Output:
http://geeksforgeeks.org
http://geeksforgeeks.org

Method 2: This method use parse_url() function to add http:// if it does not exist in the url.




<?php
  
// Declare a variable and initialize
// it with URL
$url = "geeksforgeeks.org";
  
// Display URL
echo $url;
echo "\n";
  
// Use parse_url() function
// to parse the URL
$parsed = parse_url($url);
  
// Check if parsed URL is empty
// then add http
if (empty($parsed['scheme'])) {
    $url = 'http://' . ltrim($url, '/');
}
  
// Display the URL
echo $url;
  
?>

Output:
geeksforgeeks.org
http://geeksforgeeks.org

Method 3: This method use strpos() function to add the http:// if it does not exist in the url.




<?php
  
// Declare a variable and
// initialize it
$url = "geeksforgeeks.org";
  
// Display the url
echo $url;
echo "\n";
  
// Check http:// exist in the url
// or not if not exist the add it
if(strpos($url, 'http://') !== 0) {
    $url = 'http://' . $url;
}
  
// Display the url
echo $url;
  
?>

Output:
geeksforgeeks.org
http://geeksforgeeks.org

Method 4: This method use parse_url() function to add http:// in the url if it does not exists.




<?php
  
// Declare a variable and
// initialize it
$url = "geeksforgeeks.org";
  
// Display the url
echo $url;
echo "\n";
  
// Function to add http in url
function addHttp($url, $scheme = 'http://') {
    return parse_url($url, PHP_URL_SCHEME) === null ?
            $scheme . $url : $url;
}
  
// Display URL
echo addHttp($url);
  
?>

Output:
geeksforgeeks.org
http://geeksforgeeks.org

Article Tags :