QUESTION EDITED COMPLETELY
Hello,
I'm using this code for validating URL :
$url = preg_replace("/[^A-Za-z0-9-\/\.\:]/", "", trim($url)); // clean invalid chars and space
$url = preg_replace('%^(?!https?://).*%', 'http://$0', $url); // add HTTP:// , if there isn't
if (FALSE === strpos($url, '://www.')) // if there isn't WWW
{
$url = str_replace('://', '://www.', $url); // add WWW
}
But there is a problem. If $url has a subdomain (like http://blog.example.com) , this codes still adding www (http://www.blog.example.com) .
How can i fix it ? If there is a subdomain, don't add www .