Php help needed...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 4Pics
    Confirmed User
    • Dec 2001
    • 7952

    #1

    Php help needed...

    Is there a better way to get this other then doing it in 2 steps?

    $site = preg_replace ('#www.#is', '', $_SERVER['HTTP_HOST']);
    $site = preg_replace ('#.com#is', '', $site);

    I want just the parts inbetween www and the last . where its .com/.net etc.

    Thanks
  • Serge Litehead
    Confirmed User
    • Dec 2002
    • 5190

    #2
    one line:
    $site = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], '.')+1, strpos($_SERVER['HTTP_HOST'], '.', $a+1)-strpos($_SERVER['HTTP_HOST'], '.')+1);

    the same as above but more readable:
    Code:
    $a = strpos($_SERVER['HTTP_HOST'], '.')+1;
    $b = strpos($_SERVER['HTTP_HOST'], '.', $a+1);
    
    $site = substr($_SERVER['HTTP_HOST'], $a, $b-$a);

    basically this code finds first & second periods and copies anything between them to the new variable.

    Comment

    • Serge Litehead
      Confirmed User
      • Dec 2002
      • 5190

      #3
      a bit of mix up in the above post for one liner, heres the correct one:

      $site = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], '.')+1, strpos($_SERVER['HTTP_HOST'], '.', (strpos($_SERVER['HTTP_HOST'], '.')+1)+1)-strpos($_SERVER['HTTP_HOST'], '.')+1);

      or

      $domain = $_SERVER['HTTP_HOST'];
      $site = substr($domain, strpos($domain, '.')+1, strpos($domain, '.', (strpos($domain, '.')+1)+1)-strpos($domain, '.')+1);

      Comment

      • Due
        Confirmed User
        • Mar 2001
        • 3620

        #4
        Are you looking for _SERVER["SERVER_NAME"] ??
        I buy plugs
        Skype: Due_Global
        /Due

        Comment

        • Due
          Confirmed User
          • Mar 2001
          • 3620

          #5
          Ohhh you wanted it without .com/.net or whatever
          $bla=$_SERVER["SERVER_NAME"];
          $pieces = explode(".", $bla);
          echo "domain: $pieces[0]<br>extension $pieces[1]";
          I buy plugs
          Skype: Due_Global
          /Due

          Comment

          • 4Pics
            Confirmed User
            • Dec 2001
            • 7952

            #6
            thanks

            That gave me more then 1 way to do it.

            Comment

            • BrettJ
              ol' timer
              • Jan 2001
              • 4715

              #7
              you guys are dorks ;)

              Comment

              • AgentCash
                Confirmed User
                • Feb 2002
                • 720

                #8
                preg_match('/(?:www\.)*(.*?)\./', $_SERVER['HTTP_HOST'], $sitearr);
                $site = $sitearr[1];

                Comment

                • AlCapone
                  Confirmed User
                  • Sep 2003
                  • 708

                  #9
                  "You can get more with a kind word and a gun than you can with a kind word alone.”

                  Comment

                  Working...