how to prepend www if the url string does not has www?

258 views Asked by At

In my website I have one form in that have one URL input field, so I want to check when ever the user entered url like the following combination http://example.com or www.example.com or example.com. But i want to store in my database as http://www.example.com. How can i do that using PHP not using .htaccess or jquery or javascript validation.

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

and the following code checked url validation

if (filter_var($source_url, FILTER_VALIDATE_URL) === FALSE)
{
    echo "<p class='error'>Oops! You Forgot to Enter Website URL</p>";
}
3

There are 3 answers

3
Dan Field On

Following this page and the code posted by thomas there:

<?php 

$url = 'http://usr:[email protected]:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment'; 
print append_www_to_host($url); 

function append_www_to_host($url) {
  $parsed_url = url_parse($url); 
  $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : 'http://'; 
  $host     = isset($parsed_url['host']) ? startsWith($parsed_url['host'], 'www.') ? $parsed_url['host'] : 'www.' . $parsed_url['host'] : ''; 
  $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; 
  $user     = isset($parsed_url['user']) ? $parsed_url['user'] : ''; 
  $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : ''; 
  $pass     = ($user || $pass) ? "$pass@" : ''; 
  $path     = isset($parsed_url['path']) ? $parsed_url['path'] : ''; 
  $query    = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; 
  $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; 
  return "$scheme$user$pass$host$port$path$query$fragment"; 
} 

function startsWith($haystack, $needle) {
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

?>

Note the line that starts with $host, which is where the www. is being prepended. This should print out http://usr:[email protected]:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment.

That said, I think this is a bad idea for many reasons - you may be invalidating the URL (for example, what if your user entered m.facebook.com, and now you're storing www.m.facebook.com, which doesn't exist?).

0
theHands On

Try parse_url: http://php.net/manual/en/function.parse-url.php

<?php
$parsed_url = parse_url( $url );
if( $parsed_url == false)
{
    // $url is invalid
}

if( $parsed_url['scheme'] != 'http' )
{
    $url = 'http://' . $url;
}

However, there are issues with this - what if the site is https? Or the site is on a subdomain other than www?

Might be better to check if the URL exists by attempting to load it via a file command or Curl.

See: How can I check if a URL exists via PHP?

0
Alex On

What you need is to adding www if it not exists:

$uri = "http://google.com";
if (preg_match("/http:\/\/(w){3}./" , $uri)) {
    echo "It matches...";
} else {
    echo "not match, try to add";
    if (substr($uri , 0,5) =='https') $uri = str_replace("https://" , "https://www." , $uri); else $uri = str_replace("http://" , "http://www." , $uri); 
    echo $uri;
}