How to add http:// if it doesn't exist in the URL PHP?

Here, we have set a function that adds "http:// to a string. Let’s say we have passed the following value −

And the output we want is with "http://" i.e. an actual link −

For this, you can use dot(.) notation and conditional match with preg_match().


 Live Demo

<!DOCTYPE html>
function addingTheHTTPValue($stringValue) {
   if (!preg_match("~^(?:f|ht)tps?://~i", $stringValue)) {
      $stringValue = "http://" . $stringValue;
   return $stringValue;
echo addingTheHTTPValue("");
echo "<br>";
echo addingTheHTTPValue("");