A regex solution is easy. Simply assert a negative lookahead at the start of the string like so: (With comments...)
if (preg_match('%
# Match non-http ,com or .net domain.
^ # Anchor to start of string.
(?! # Assert that this URL is NOT...
https?:// # HTTP or HTTPS scheme with
(?:www\.)? # optional www. subdomain.
) # End negative lookahead.
.* # Match up to TLD.
\. # Last literal dot before TLD.
(?: # Group for TLD alternatives.
net # Either .net
| com # or .com.
) # End group of TLD alts.
$ # Anchor to end of string.
%xi', $text)) {
// It matches.
} else {
// It doesn't match.
}
Note that since: http://www.
is a subset of: http://
, the expression for the optional www.
is not necessary. Here is a shorter version:
if (preg_match('%^(?!https?://).*\.(?:net|com)$%i', $text)) {
// It matches.
} else {
// It doesn't match.
}
Simple regex to the rescue!