0

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 .

Eray
  • 7,038
  • 16
  • 70
  • 120

1 Answers1

2

I think, substr is actually supposed to be strpos?

I doubt this code ever worked. Since you're not checking for identity (===), the condition is always true, thus prepends www.. That should work however:

if (FALSE === strpos($url, '://www.'))
   $url = str_replace('://', '://www.', $url);

There's no need to replace using expensive regular expressions in this case, so you should use str_replace.


UPDATE: The question had been edited. I suggest the following:

// Strip "invalid" characters
$url = preg_replace('/[^a-z0-9\.\-]/i', '', $url);

// Split URL by scheme, host, path (and possibly more)
$parts = parse_url($domain);

if (empty($parts['scheme']))
   $parts['scheme'] = 'http';
if (!strcmp('example.com', $parts['host']))
   $parts['host'] = 'www.example.com';

// Reconstruct URL
$url = sprintf('%s://%s%s', $parts['scheme'], $parts['host'], $parts['path']);

Be aware, that parse_url may return a lot more. You'll need to reconstruct accordingly.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
  • @Eray No. You're checking to see, if there's an occurence of "://www." and if not, insert this. So you need to check whether [`strpos`](http://de3.php.net/manual/en/function.strpos.php) returns `FALSE` (not found). – Linus Kleen Jan 24 '11 at 10:46
  • @goreSplatter , thank you very much now problem solved with this method. Can i ask a little something ? when `$url = "sdfsdf"` , `$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);` returning **200 (OK)** , but as you can see *sdfsfd* isn't an URL. Why it's returning 200 ? – Eray Jan 24 '11 at 10:51
  • @Eray [You asked that before](http://stackoverflow.com/questions/4627577/200-http-code-at-non-url-strings/4627641#4627641). See my last comment on that answer. – Linus Kleen Jan 24 '11 at 10:54
  • @goreSplatter, we forgot something. If there is a subdomain, **www** sholdn't added. How can we do this ? – Eray Jan 24 '11 at 14:04