Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    If, does not match, (no else)

    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.
    PHP Code:
    if (!(preg_match('/.*domain.com$/si'$_SERVER['SERVER_NAME'])))
    {
    // Do Something

    Last edited by sonny; 02-24-2012 at 11:14 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    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?
    PHP Code:
    if (substr('domain.com', -101); != $_SERVER['SERVER_NAME']) 
    this is confusing as heck to me, looking for the most efficient way to
    accomplish this.

    Thanks
    Sonny
    Last edited by sonny; 02-23-2012 at 11:38 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, strpos:
    PHP Code:
    if (false !== strpos($_SERVER['SERVER_NAME'], 'domain.com'))
    {
        print 
    'server_name contains domain.com';

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

  • Users who have thanked Fou-Lu for this post:

    sonny (02-24-2012)

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    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...

    PHP Code:
    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
    PHP Code:
    if (fValidateDomain($_SERVER['SERVER_NAME']) {
        
    // Do your stuff

    Otherwise

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

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


  • Users who have thanked Serex for this post:

    sonny (02-24-2012)

  • #6
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    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

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •