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>";
}
Following this page and the code posted by thomas there:
Note the line that starts with
$host
, which is where thewww.
is being prepended. This should print outhttp://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 storingwww.m.facebook.com
, which doesn't exist?).