...

View Full Version : Resolved If, does not match, (no else)



sonny
02-23-2012, 06:29 PM
Hi I am trying to run code if the domain does not match, "without using a else"

I tried this below but it does not work.


if (!(preg_match('/.*domain.com$/si', $_SERVER['SERVER_NAME'])))
{
// Do Something
}

Fou-Lu
02-23-2012, 07:36 PM
Works fine for me. Aside from the .com likely being wrong, that should be \.com, although . does happen to match. . . dot. Lol.
If you know explicitly what you are looking for, using strpos would probably be a better solution to searching in a string.

sonny
02-23-2012, 11:17 PM
Hi
I am trying to just check on the domain.com part with that preg_match
either of theses below is considered OK
http://domain.com
http://www.domain.com
domain.com

how can I do that with strpos based on domain.com for example

or should I use something like this and just count back 10?


if (substr('domain.com', -10, 1); != $_SERVER['SERVER_NAME'])


this is confusing as heck to me, looking for the most efficient way to
accomplish this.

Thanks
Sonny

Fou-Lu
02-24-2012, 05:25 AM
No, strpos:


if (false !== strpos($_SERVER['SERVER_NAME'], 'domain.com'))
{
print 'server_name contains domain.com';
}


Swap it to === to check if it doesn't contain.

Serex
02-24-2012, 07:42 AM
No real ability to test this where I am at the moment but if you feel you must use expressions then you could do something like...


function fValidateDomain($name)
{
preg_match("/(http(s)?:\/\/)?((www\.)?([^\/]+)\.([\w+]{0,4}))(\/(.*))?/", $name, $check);
$chkDomains = array("http://www.domain.com", "www.domain.com", "domain.com");

if (!empty($check[3])) {
if ($chkDomains) {
if (in_array($check[3], $chkDomains)) {
return true;
}
else {
return false;
}
} else {
return false;
}
} else {
return true;
}
}

Then just call it and parse the domain in


if (fValidateDomain($_SERVER['SERVER_NAME']) {
// Do your stuff
}


Otherwise



$chkDomains = array("http://www.domain.com", "www.domain.com", "domain.com");

if (in_array($_SERVER['SERVER_NAME'], $chkDomains)) {
// Do something
}

sonny
02-24-2012, 11:11 PM
Thanks Guys
foo your way was way more efficient then the way I was trying to do it.

Serex, good one, I can't believe I did not even think about the array approach.
wonder whats fastest out of the two above,

from what I read, preg_match is by far, the more resource extensive then either.

Thanks
Sonny

Fou-Lu
02-24-2012, 11:17 PM
Strings are always the fastest.
Then arrays.
Then at the very far back, regex.

The differences come from the usage. I know I want to find domain.com for example within a string. There is no need to pull full comparisons on everything (and will grow with subdomains and complexity increases if I add subdirectories from a filepath) for just that purpose. If I wanted to find specific matches such as only http://domain.com/directory/index.php, http://sub.directory.com/index.php, ... then an in_array would make the most sense. If I want to find if something is a domain format, but don't know what it may be and just want to capture that data, I would use a regex.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum